标签:
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 22283 Accepted Submission(s): 8106
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int N = 200005; const int M = 10000001; int Hash[M]; int cnt[N]; int father[N],dep[N]; int _find(int x){ if(x==father[x]) return x; return _find(father[x]); } int Union(int a,int b){ int x = _find(a); int y = _find(b); if(x==y) return 0; if(dep[x]==dep[y]){ father[x] = y; dep[y]++; }else if(dep[x]<dep[y]){ father[x] = y; }else father[y]=x; return 1; } int main() { int n; while(scanf("%d",&n)!=EOF){ if(n==0) { printf("1\n"); continue; } memset(Hash,-1,sizeof(Hash)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=2*n;i++){ father[i] = i; dep[i]=0; } int k=0; for(int i=0;i<n;i++){ int a,b; scanf("%d%d",&a,&b); if(Hash[a]==-1){ Hash[a]=++k; } if(Hash[b]==-1){ Hash[b]=++k; } Union(Hash[a],Hash[b]); } for(int i=1;i<=2*n;i++){ cnt[_find(i)]++; } int Max = -1; for(int i=1;i<=2*n;i++){ Max = max(Max,cnt[i]); } printf("%d\n",Max); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5474598.html