标签:
简单并查集,985ms险过,注意n=0时输出1
#include<iostream> #include<cstring> #define maxn 10000000+5 using namespace std; int a[maxn],b[maxn]; int dfs(int x) { if(a[x]!=x) { a[x]=dfs(a[x]); } return a[x]; } int main() { int n; while(cin>>n) { if(!n){cout<<1<<endl;continue;} for(int i=1;i<maxn;i++) a[i]=i,b[i]=0; while(n--) { int x,y; cin>>x>>y; if(dfs(x)!=dfs(y)) { a[dfs(y)]=dfs(x); } } int re=0; for(int i=1;i<maxn;i++) b[dfs(i)]++,re=max(re,b[dfs(i)]); cout<<re<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/44941171