Mr Wang wants some boys to help him with a project. Because the project is rather complex,
the more boys come, the better it will be. Of course there are certain requirements.
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.
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.
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
4 2
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.
题目的大意就是找到最大的集合然后输出
很明显的一个并查集。遵循并查集的套路就好了,加一个ran[]数组记录所连接的人数。
代码:
#include
#include
#include
#define maxn 10000000+10
using namespace std;
int p[maxn];
int ran[maxn];
int ma;
void init()
{
for(int i=1;i<=maxn;i++)
p[i]=i,ran[i]=1;
}
int find_(int x)
{
if(x!=p[x])
p[x]=find_(p[x]);
return p[x];
}
void mix(int x,int y)
{
int xx=find_(x);
int yy=find_(y);
if(xx!=yy)
{
p[xx]=yy;
ran[yy]+=ran[xx];
if(ran[yy]>ma)
ma=ran[yy];
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
ma=1;
else
{
init();
int a,b;
ma=0;
for(int i=0;i