Description
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.
Input
Output
Sample Input
Sample Output
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
#include<stdio.h>
int bin[100005],num[100005];
int find(int x)
{
int r;
r=x;
while(bin[r]!=r)
r=bin[r];
return r;
}
void bing(int x,int y)
{ int fx,fy;
fx=find(x);
fy=find(y);
if(fx==fy) return ;
else if(fx<=fy)
{
bin[fx]=fy;
num[fy]+=num[fx];
}
else if(fx>=fy)
{
bin[fy]=fx;
num[fx]+=num[fy];
}
}
int main()
{
int n,x,y,d;
while(scanf("%d",&n)!=EOF)
{ d=1;
for(int i=1;i<=100005;i++)
{
bin[i]=i;
num[i]=1;
}
for(int j=1;j<=n;j++)
{
scanf("%d%d",&x,&y);
bing(x,y);
}
for(int i=1;i<=100005;i++)
{
if(d<num[i]) d=num[i];
}
printf("%d\n",d);
}
return 0;
}