Time Limit:1000MS | Memory Limit:10000K | |
Total Submissions:19801 | Accepted:5655 |
Description
Input
Output
Sample Input
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
源码/////////////////////
#include<stdio.h> #include<stdlib.h> int lev[5000][5000]; int father[50000]; int rank[50000]; int sum; void initial(); int Find_Set(int x); int Union(int x,int y,int len); void initial() { sum=0; memset(rank,0,sizeof(rank)); int _i; for(_i=0;_i<50000;_i++) father[_i]=_i; } int Find_Set(int x) { int t; if (father[x] == x) return x; t = father[x]; father[x] = Find_Set(father[x]); rank[x] = (rank[t] + rank[x]) % 3; return father[x]; } int Union(int x,int y,int len) { int a,b; a=Find_Set(x); b=Find_Set(y); if(a==b) return 0; else { father[a]=b; rank[a]=(rank[y]-rank[x]+3+len)%3; } return 1; } int main() { int n,k; scanf("%d %d",&n,&k); int _i,a,b,rela; int x,y; initial(); for(_i=0;_i<k;_i++) { scanf("%d %d %d",&rela,&x,&y); if(x>n||y>n||(rela==2&&x==y)) sum++; else { a=Find_Set(x),b=Find_Set(y); if(a!=b) Union(x,y,rela-1); else { if(rela-1!=(rank[x]-rank[y]+3)%3) sum++; } } } printf("%d/n",sum); return 0; }