标签:
1 #include<cstdio> 2 using namespace std; 3 4 const int maxn=10005; 5 6 int bin[maxn],rank[maxn],cnt[maxn]; 7 8 int find(int x) 9 { 10 if(x==bin[x]) return x; 11 int t=bin[x]; 12 bin[x] = find(bin[x]); 13 cnt[x] += cnt[t]; 14 return bin[x]; 15 } 16 17 18 void merge(int x,int y) 19 { 20 int fx=find(x); 21 int fy=find(y); 22 if(fx!=fy) 23 { 24 bin[fx]=fy; 25 rank[fy]+=rank[fx]; 26 cnt[fx]=1; 27 } 28 } 29 30 int main() 31 { 32 //freopen("in.txt","r",stdin); 33 char c; 34 int t,kase,n,q,i,a,b; 35 scanf("%d",&t); 36 kase=1; 37 while(t--) 38 { 39 printf("Case %d:\n",kase++); 40 scanf("%d%d",&n,&q); 41 for(i=1;i<=n;i++) 42 { 43 bin[i]=i; 44 cnt[i]=0; 45 rank[i]=1; 46 } 47 while(q--) 48 { 49 getchar(); 50 scanf("%c%d",&c,&a); 51 if(c==‘T‘) 52 { 53 scanf("%d",&b); 54 merge(a,b); 55 } 56 else 57 { 58 int temp=find(a); 59 printf("%d %d %d\n",bin[temp],rank[temp],cnt[a]); 60 } 61 } 62 } 63 return 0; 64 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4694303.html