一个简单的并查集,唯一的可能wa的点是考虑只有一个人的情况(0对朋友,可以选一人)。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5;
int num[maxn], cnt[maxn];
int pre[maxn], n, ans;
int root(int x)
{
if(x != pre[x])
{
pre[x] = root(pre[x]);
}
return pre[x];
}
int merge_set(int x, int y)
{
int fx = root(x);
int fy = root(y);
if(fx != fy)
{
cnt[fx] = cnt[fx] + cnt[fy];
pre[fy] = fx;
//printf("%d %d\n", fx, cnt[fx]);
ans = max(ans, cnt[fx]);
}
}
int main()
{
while(scanf("%d", &n) == 1)
{
ans = 1;
for(int i = 0; i < maxn; i++)
pre[i] = i;
for(int i = 0; i< maxn; i++)
cnt[i] = 1;
for(int kase = 0; kase < n; kase++)
{
int t1, t2;
scanf("%d%d", &t1, &t2);
merge_set(t1, t2);
}
printf("%d\n", ans);
}
return 0;
}