标签:des style blog http java color
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2839 Accepted Submission(s): 1097
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 8 int father[10005]; 9 int timee[10005]; 10 int num[10005]; 11 12 int findroot(int p){ 13 if(p==father[p]) return p; 14 int f=father[p]; 15 father[p]=findroot(father[p]); 16 timee[p]+=timee[f]; //把沿途上所有与点p有顺带关系的点的移动次数加到点p上 (好好调试跟踪下) 17 return father[p]; 18 } 19 20 void unsion(int x,int y){ 21 int fx=findroot(x); 22 int fy=findroot(y); 23 if(fx!=fy){ //每次都以新的土著龙珠为根结点 24 num[fy]+=num[fx]; 25 father[fx]=fy; 26 timee[fx]=1; 27 } 28 } 29 30 main() 31 { 32 int t, i, j, x, y, n, Q, kase=1; 33 char s[10]; 34 cin>>t; 35 while(t--){ 36 scanf("%d %d",&n,&Q); 37 printf("Case %d:\n",kase++); 38 for(i=1;i<=n;i++){ 39 father[i]=i; 40 timee[i]=0; 41 num[i]=1; 42 } 43 while(Q--){ 44 scanf("%s",s); 45 if(strcmp(s,"T")==0){ 46 scanf("%d %d",&x,&y); 47 unsion(x,y); 48 } 49 else { 50 scanf("%d",&x); 51 int fx=findroot(x); 52 printf("%d %d %d\n",fx,num[fx],timee[x]); 53 } 54 } 55 } 56 }
HDU 3635 延缓更新的并查集,布布扣,bubuko.com
标签:des style blog http java color
原文地址:http://www.cnblogs.com/qq1012662902/p/3855185.html