Advertisement
Guest User

Untitled

a guest
Jun 28th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include<cstdio>
  2. #include<algorithm>
  3. #define MAX_M 300010
  4. #define MAX_N 7010
  5. using namespace std;
  6.  
  7. int n,m;
  8. bool ans[MAX_M];
  9. int head[MAX_N];
  10.  
  11. struct edge_tag {
  12.     int x,y,w;
  13.     int id;
  14. } edge[MAX_M];
  15.  
  16. bool cmp( edge_tag a , edge_tag b )
  17. {
  18.     return a.w < b.w;
  19. }
  20.  
  21. int findhead(int h)
  22. {
  23.     if( head[h] != h )  head[h] = findhead( head[h] );
  24.     return head[h];
  25. }
  26.  
  27. int main()
  28. {
  29.     scanf("%d%d",&n,&m);
  30.     for(int c=1;c<=n;c++) head[c] = c;
  31.     for(int c=1;c<=m;c++)
  32.     {
  33.         scanf("%d%d%d",&edge[c].x,&edge[c].y,&edge[c].w);
  34.         edge[c].id = c;
  35.     }
  36.     sort( edge+1 , edge+m+1 , cmp );
  37.     for(int c=1;c<=m;c++)
  38.     {
  39.         if( edge[c].w != edge[c-1].w )
  40.         {
  41.             for(int d=c;d<=m and edge[d].w==edge[c].w;d++) if( findhead( edge[d].x ) != findhead( edge[d].y ) ) ans[ edge[d].id ] = true;
  42.         }
  43.         int x = findhead( edge[c].x ), y = findhead( edge[c].y );
  44.         head[x] = head[y] = min( x ,y );
  45.     }
  46.     for(int c=1;c<=m;c++) if( ans[c] == 1 ) printf("TAK\n"); else printf("NIE\n");
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement