标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4384 Accepted Submission(s):
1673
#include<stdio.h> #include<string.h> #define MAX 20000 int set[MAX]; int path[MAX]; int time[MAX]; int find(int fa) { int t; if(fa==set[fa]) return fa; t=set[fa]; set[fa]=find(set[fa]); time[fa]+=time[t]; return set[fa]; } void mix(int x,int y) { int fx; int fy; fx=find(x); fy=find(y); if(fx!=fy) { set[fx]=fy; path[fy]+=path[fx]; time[fx]++; } } int main() { int t,n,m,x,y,b,i; char a; scanf("%d",&t); int k=0; while(t--) { scanf("%d%d",&n,&m); printf("Case %d:\n",++k); for(i=1;i<=n;i++) { set[i]=i; path[i]=1; time[i]=0; } while(m--) { getchar(); scanf("%c %d",&a,&x); if(a==‘T‘) { scanf("%d",&y); mix(x,y); } else { y=find(x);//此处必须用一个变量值来表示find(x) printf("%d %d %d\n",y,path[y],time[x]); } } } return 0; }
hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4700381.html