题目大意是要找出一个朋友集合,这个朋友集合的大小最大,输出这个集合的大小
朋友的朋友是朋友,这个很自然就会想到并查集,这里的并查集要用到路径压缩,因为点数太多,
集合的大小也好处理,只要在每次合并集合时增加作为祖先的元素所对应的大小就好了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <stack>
using namespace std;
typedef long long ll;
const int NIL = -1;
const int MAXN = 10000005;
const int INF = 0x3f3f3f3f;
int Size[MAXN];
int p[MAXN];
int ans;
int find_parent(int x)
{
return p[x] == x ? x : p[x] = find_parent(p[x]);
}
void Union(int x, int y)
{
int a = find_parent(x);
int b = find_parent(y);
if (a != b)
{
p[a] = b;
Size[b] += Size[a];
ans = max(ans, Size[b]);
}
}
int main(void)
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int n, a, b;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < MAXN; ++i)
p[i] = i, Size[i] = 1;
ans = 1;
while ( n-- )
{
scanf("%d%d", &a, &b);
Union(a, b);
}
printf("%d\n", ans);
}
return 0;
}