标签:
Time Limit: 5000/1000 MS (Java/Others)
Memory Limit: 327680/102400 K (Java/Others)
1 #include<bits/stdc++.h> 2 using namespace std; 3 int pre[10000005],r[10000005],n,maxx; 4 bool vis[10000005]; 5 int find(int x) { 6 int r=x; 7 while(pre[r]!=r) r=pre[r]; 8 int i=x,j; 9 while(i!=r) { 10 j=pre[i]; 11 pre[i]=r; 12 i=j; 13 } 14 return r; 15 } 16 void join(int a,int b) { 17 int a1=find(a),b1=find(b); 18 if (a1!=b1) { 19 pre[a1]=b1; 20 r[b1]+=r[a1]; 21 if (r[b1]>maxx) maxx=r[b1]; 22 } 23 } 24 25 int main() { 26 while(scanf("%d",&n)!=EOF) { 27 maxx=-1;memset(vis,0,sizeof(vis)); 28 if(n==0) {printf("1\n"); continue;} 29 for (int i=1;i<=n;++i) { 30 int a,b; 31 scanf("%d%d",&a,&b); 32 if(!vis[a]) { 33 vis[a]=1; 34 pre[a]=a; 35 r[a]=1; 36 } 37 if(!vis[b]) { 38 vis[b]=1; 39 pre[b]=b; 40 r[b]=1; 41 } 42 join(a,b); 43 } 44 printf("%d\n",maxx); 45 } 46 return 0; 47 }
标签:
原文地址:http://www.cnblogs.com/TonyNeal/p/hdu1856.html