Advertisement
TLE

UVA - 1174 - IP-TV (AC)

TLE
Nov 12th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define pf(x) printf("%d",x)
  6. #define pfd(x,y) printf("%d %d",x,y)
  7. #define pfl(x) printf("%ld",x)
  8. #define nl printf("\n")
  9.  
  10. #define cs(x) printf("Case %d: ",x)
  11. #define csn(x) printf("Case %d:\n",x)
  12.  
  13. #define all(x) x.begin(),x.end()
  14. #define Find(x,n) find(all(x),n)
  15. #define pi acos(-1.0)
  16. #define i64 long long
  17. #define pb(x) push_back(x)
  18. #define mset(x,v) memset(x,v,sizeof(x))
  19. #define sc(n) scanf("%d",&n)
  20. #define scl(n) scanf("%ld",&n)
  21. #define scll(n) scanf("%lld",&n)
  22. #define scd(n,m) scanf("%d %d",&n,&m)
  23. #define sct(n,m,w) scanf("%d %d %d",&n,&m,&w)
  24. #define scdl(n,m) scanf("%ld %ld",&n,&m)
  25. #define scdll(n,m) scanf("%lld %lld",&n,&m)
  26. #define filein freopen("in.txt","r",stdin)
  27. #define fileout freopen("my.txt","w",stdout)
  28. #define FOR(i,n) for(int i=0;i<n; i++)
  29. #define inf 100000000
  30. #define MAX 200010
  31.  
  32.  
  33. bool isUpper(char ch){ if( ch>='A' && ch<='Z' ) return true; return false; }
  34. bool isLower(char ch){ if( ch>='a' && ch<='z') return true; return false;}
  35. bool isLetter(char ch){ if( ch>='A' && ch<='Z' || ch>='a' && ch<='z') return true; return false; }
  36. bool isDigit(char ch){ if( ch>='0' && ch<='9') return true; return false; }
  37. char toLower(char ch){ if (isUpper(ch)) return (ch+32); return ch; }
  38. char toUpper(char ch){ if (isLower(ch)) return (ch-32); return ch; }
  39.  
  40. template<class T>bool isEven(T a){ return (a%2==0);}
  41. template<class T>T sq(T a){ return a*a; }
  42. template<class T>T gcd(T a,T b){ return b==0 ? a : gcd(b,a%b); }
  43. template<class T>T lcm(T a,T b){ return (a/gcd(a,b))*b; }
  44. template<class T>bool isPrime(T n){ for(T i=2; i*i<=n; i++){ if(n%i==0) return false; } return true; }
  45. template<class T>T Pow(T n,T p) { T res=n; for(T i=1;i<p; i++){ res *= n; } return res; }
  46. template<class T>T Max(T n,T p) { if(n>=p) return n; return p; }
  47.  
  48. struct node
  49. {
  50.     int u,v,w;
  51.     node( int U,int V,int W ){ u=U; v=V; w=W; }
  52.     bool operator < ( const node& p) const { return w < p.w; }
  53. };
  54.  
  55. vector<node>g;
  56. int par[MAX];
  57. map<string,int>id;
  58.  
  59. int parent(int x){
  60.     return (par[x]==x) ? x : par[x]=parent(par[x]);
  61. }
  62.  
  63. int mst(int n){
  64.     for(int i=0;i<=n;i++) par[i]=i;
  65.     int sz=(int)g.size();
  66.     int nodes=0;
  67.     int res=0;
  68.     for(int i=0;i<sz;i++){
  69.         int u=parent(g[i].u);
  70.         int v=parent(g[i].v);
  71.         if(u!=v){
  72.             par[u]=v;
  73.             res += g[i].w;
  74.             nodes++;
  75.             if(nodes == n-1) return res;
  76.         }
  77.     }
  78.     return res;
  79. }
  80.  
  81. int main()
  82. {
  83.     //filein;
  84.     //fileout;
  85.  
  86.     int t;
  87.     sc(t);
  88.     while( t-- ){
  89.         int n,m;
  90.         scd(n,m);
  91.         int index=1;
  92.         for(int i=0;i<m;i++){
  93.             string u,v;
  94.             int w;
  95.             cin >> u >> v;
  96.             sc(w);
  97.             if(id[u]==0) id[u]=index++;
  98.             if(id[v]==0) id[v]=index++;
  99.             g.pb( node(id[u],id[v],w) );
  100.         }
  101.         sort( all(g) );
  102.         printf("%d\n",mst(n));
  103.         if(t) nl;
  104.         id.clear();
  105.         g.clear();
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement