hdu1856 More is better 基础并查集

时间:2023-01-06 16:40:16
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std; #define MAXN 100005
int fa[MAXN]; //父节点
int vis[MAXN];
int Min = 0xffffff;
int Max = ; void init()
{
for (int i = ; i < MAXN; i++)
{
fa[i] = i;
}
} int find(int x)
{
if (x == fa[x])
return x;
else
return fa[x] = find(fa[x]);
} void set_union(int a, int b)
{
int a1 = find(a);
int b1 = find(b); if (a1 == b1)
return;
else
{
if (a1 < b1)
fa[b1] = a1;
else
fa[a1] = b1;
}
} int main()
{
int n;
while (~scanf("%d",&n))
{
init();
for (int i = ; i < n; i++)
{
int a, b;
scanf("%d %d", &a, &b); Min = min(Min, min(a, b));
Max = max(Max, max(a, b)); set_union(a, b);
} int ans = ;
memset(vis, , sizeof(vis));
for (int i = Min; i <= Max; i++)
{
vis[find(i)]++;
if (ans < vis[find(i)])
ans = vis[find(i)];
}
printf("%d\n", ans);
}
//system("pause");
return ;
}