标签:
解题思路:将给出的男孩的关系合并后,另用一个数组a记录以find(i)为根节点的元素的个数,最后找出数组a的最大值
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 15861 Accepted Submission(s): 5843
#include<stdio.h> #include<string.h> int pre[10000010],a[10000010]; int find( int root) { if(root!=pre[root]) pre[root]=find(pre[root]); return pre[root]; } void unionroot( int root1,int root2) { int x,y; x=find(root1); y=find(root2); if(x!=y) pre[x]=y; } int main() { int n; int root1,root2,x,y,k,i,max; while(scanf("%d",&n)!=EOF) { max=-100000; for(i=1;i<=10000010;i++) a[i]=0; for(i=1;i<=10000010;i++) pre[i]=i; while(n--) { scanf("%d %d",&root1,&root2); x=find(root1); y=find(root2); unionroot(x,y); } for(i=1;i<=10000010;i++) { k=find(i); a[k]++;//记录以find(i)为根节点的包含有多少 个元素 } for(i=1;i<=10000010;i++) { if(a[i]>max) max=a[i]; } printf("%d\n",max); } }
标签:
原文地址:http://www.cnblogs.com/wuyuewoniu/p/4202541.html