codeup 问题 D More is better
#include <cstdio>
#include <cstring>
#include <cassert>
#define MAXN (10000000)
int father[MAXN+5];
int num[MAXN+5];
int N;
int ans=1;
int Find(int x) {
int a=x;
while (father[x] != x) {
x=father[x];
}
while (father[a] != a) {
int temp=father[a];
father[a]=x;
a=temp;
}
return x;
}
void Union(int a, int b) {
int faA=Find(a);
int faB=Find(b);
if (faA != faB) {
father[faA]=faB;
num[faB]+=num[faA];
num[faA]=0;
if (num[faB] > ans) {
ans=num[faB];
}
}
}
int main(int argc, char** argv) {
while (scanf("%d", &N) != EOF) {
ans=1;
for (int i=1;i<=MAXN;++i) {
father[i]=i;
num[i]=1;
}
while (N--) {
int A, B;
scanf("%d%d", &A, &B);
Union(A, B);
}
assert(ans > 0);
printf("%d\n", ans);
}
return 0;
}