标签:des style blog color os strong io for
并查集...思路很简单...求生成的集合中最大的集合所含的元素个数...
WA了八百年...最后发现当输入 0 时,男孩没有朋友,应输出 1 .
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #define maxn 10000005; 5 int fa[maxn], sum[maxn]; 6 int ans; 7 8 int find(int x){ 9 if(fa[x] == x) 10 return fa[x]; 11 else 12 return fa[x]; 13 } 14 15 int main(){ 16 int t; 17 while(scanf("%d", &t) != EOF){ 18 if(t==0){ 19 printf("1\n"); 20 continue; 21 } 22 ans = 0; 23 for(int i = 1; i < maxn; i++){ 24 sum[i]=1; 25 fa[i]=i; 26 } 27 int x,y; 28 while(t--){ 29 scanf("%d%d",&x,&y); 30 int fa_x, fa_y; 31 fa_x = find( x ); 32 fa_y = find( y ); 33 if(sum[fa_x] < sum[fa_y]){ 34 fa[fa_x] = fa_y; 35 sum[fa_y] += sum[fa_x ]; 36 ans = fmax(ans, sum[fa_y]); 37 } 38 else{ 39 fa[fa_y] = fa_x; 40 sum[fa_x] += sum[fa_y]; 41 ans = fmax(ans, sum[fa_x]); 42 } 43 } 44 printf("%d\n",ans); 45 } 46 return 0; 47 }
标签:des style blog color os strong io for
原文地址:http://www.cnblogs.com/zzy9669/p/3875292.html