标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 21167 Accepted Submission(s): 7720
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <set> 6 #include <cmath> 7 using namespace std; 8 #define Max 10000000+5 9 int per[Max]; 10 int ans[Max]; 11 int num[Max]; 12 bool vis[Max]; 13 int n; 14 void init() 15 { 16 for(int i=1;i<Max;i++) 17 per[i]=i; 18 } 19 int find(int a) 20 { 21 if(a==per[a]) 22 return a; 23 return per[a]=find(per[a]); 24 } 25 int unite(int a,int b) 26 { 27 a=find(a); 28 b=find(b); 29 if(a!=b) 30 per[a]=b; 31 return 0; 32 } 33 int main() 34 { 35 int t,a,b; 36 freopen("in.txt","r",stdin); 37 while(scanf("%d",&n)!=EOF) 38 { 39 init(); 40 int Maxn=1,p=0; 41 memset(vis,0,sizeof(vis)); 42 fill(ans,ans+Max,0); 43 for(int i=0;i<n;i++) 44 { 45 scanf("%d%d",&a,&b); 46 if(vis[a]==0) 47 { 48 vis[a]=1; 49 num[p++]=a; 50 } 51 if(vis[b]==0) 52 { 53 vis[b]=1; 54 num[p++]=b; 55 } 56 unite(a,b); 57 } 58 for(int i=0;i<p;i++) 59 { 60 t=find(num[i]); 61 ans[t]++; 62 //cout<<t<<endl; 63 Maxn=max(Maxn,ans[t]); 64 } 65 printf("%d\n",Maxn); 66 } 67 }
More is better(hdu 1856 计算并查集集合中元素个数最多的集合)
标签:
原文地址:http://www.cnblogs.com/a1225234/p/5181339.html