hdu1856
More is better
0x00 Tags
并查集
0x01 题目简介
本质:求解最大的集合的长度
0x02 代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 10000010;
int root[maxn], num[maxn];
void Init()
{
for (int i = 1; i < maxn; i++)
{
root[i] = i;
num[i] = 1;
}
}
int Find(int x)
{
while (x != root[x])
{
x = root[x];
}
return x;
}
void Union(int x, int y)
{
int fx = Find(x);
int fy = Find(y);
if (fx != fy)
{
root[fx] = fy;
num[fy] += num[fx];
}
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
if (n <= 0) break;
Init();
int a, b, ans = -1;
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &a, &b);
Union(a, b);
}
for (int i = 1; i < maxn; i++)
{
if (num[i] > ans) ans = num[i];
}
printf("%d
", ans);
}
return 0;
}