题意:1×10^7个男孩,找出互相之间是朋友关系的人数最多的集合的人数,如果N = 0 ,输出1;
值得一提的是,用C++交怎么也是CE(因为数组过大),G++交就是没事
#include<stdio.h>
#include<iostream>
using namespace std;
int father[10000005];
int c[10000005];
void init(int n)//初始化
{
for(int i = 1 ; i <= n ; i ++)
{
father[i] = i;
c[i] = 1;
}
}
int find(int x)
{
int i = x;
while(father[i] != i)
{
i = father[i];
}
int r = x;
int j ;
while(father[r]!=r)
{
j = father[r];
father[r] = i;
r = j;
}
return i;
}
int maxx ;
void merge(int x,int y)
{//合并操作
int fx = find(x);
int fy = find(y);
if(fx!=fy)
{
father[fx] = fy;
c[fy]+=c[fx];
}
maxx=max(c[fy],maxx);
}
int main()
{
int n,m;
n = 10000000;
int x,y;
while(scanf("%d",&m)!=EOF)
{
if(m==0)
cout<<1<<endl;
else
{
maxx =0;
init(n);
for(int i = 1 ; i <= m; i ++)
{
scanf("%d %d",&x,&y);
merge(x,y);
}
cout<<maxx<<endl;
}
}
return 0;
}