HDU 1856 More is better
找最大连通分量,没有一对pairs
就输出1
。
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 1e7 + 2;
int pre[MAXN];
int pairs;
int opt;
int f(int x)
{
int f0 = x, f1 = x;
for (; pre[f0] > 0;)
f0 = pre[f0];
for (; pre[f1] > 0;)
{
int t = f1;
f1 = pre[f1];
pre[t] = f0;
}
return f0;
}
void u(int a, int b)
{
int f1 = f(a);
int f2 = f(b);
if (f1 != f2)
{
if (pre[f1] <= pre[f2])
{
pre[f1] += pre[f2];
if (-pre[f1] > opt) opt = -pre[f1];
pre[f2] = f1;
}
else
{
pre[f2] += pre[f1];
if (-pre[f2] > opt) opt = -pre[f2];
pre[f1] = f2;
}
}
}
int main()
{
for (; ~scanf("%d", &pairs);)
{
int a, b;
memset(pre, -1, sizeof pre);
opt = 1; // n=0 时直接输出 1 又是这种坑点,服
for (int i = 0; i < pairs; i++)
{
scanf("%d%d", &a, &b);
u(a, b);
}
printf("%d\n", opt);
}
return 0;
}