More is better
Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
#include<stdio.h>
#define MAX 10000002
int father[MAX];
int rank[MAX];
void Make_Set(void)
{
int i;
for(i=1;i<=MAX;i++)
{
father[i]=i;
rank[i]=1;
}
}
int Find_Set(int x)//查找x元素所在的集合,回溯时压缩路径
{
if(x!=father[x])
{
father[x]=Find_Set(father[x]);//这个回溯的压缩路径是精华
}
return father[x];
}
void Union(int x,int y)//合并
{
x=Find_Set(x);
y=Find_Set(y);
if(x!=y)
{
father[x]=y;
rank[y]+=rank[x];
}
}
int main()
{
int t,x,y,i,n,m;
while(scanf("%d",&t)!=EOF)
{
Make_Set();
int max=0;
while(t--)
{
scanf("%d%d",&n,&m);
Union(n,m);
}
for(i=1;i<=MAX;i++)
{
if(father[i]==i)
{
if(rank[i]>max)
max=rank[i];
}
}
printf("%d\n",max);
}
return 0;
}