水题一个,1A,手懒,跑得有点慢,,C++ 500的样子。G++请别递归,否则可能不行,其实优化也很容易,不做赘述了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 10000000+10;
int father[maxn];
int cnt[maxn];
int getfather(int x)
{
if (father[x] != x)
{
father[x] = getfather(father[x]);
}
return father[x];
}
void merge(int a,int b)
{
int fa = getfather(a);
int fb = getfather(b);
if (fa != fb)
{
father[fb] = fa;
}
}
int main(int argc, char const *argv[])
{
int m;
while(scanf("%d",&m)!=EOF)
{
for (int i = 0; i < maxn; ++i)
{
cnt[i] = 0;
father[i] = i;
}
for (int i = 0; i < m; ++i)
{
int a,b;
scanf("%d%d",&a,&b);
merge(a,b);
}
for (int i = 0; i < maxn; ++i)
{
cnt[getfather(i)]++;
}
sort(cnt,cnt+maxn);
printf("%d\n",cnt[maxn-1]);
}
return 0;
}