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
#include<stdio.h>
const int M = 10000000 + 10;
int num,vis[M],par[M];
int find(int x)
{
return x==par[x]?x:par[x]=find(par[x]);
}
void unite(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
vis[fx]+=vis[fy];
par[fy]=fx;
}
}
int main()
{
int n,m;
while(~scanf("%d",&n)){
m=n;
for(int i=0;i<M;i++) {
par[i]=i;
vis[i]=1;
}
num=1;
int x,y;
while(m--){
scanf("%d%d",&x,&y);
unite(x,y);
}
printf("%d\n",num);
}
}
本文介绍了一道算法题目,核心在于找出最大数量的好友群组。通过输入一系列直接好友关系,利用并查集算法来解决该问题。最终输出王先生可以保留的最大男孩数量。
2398

被折叠的 条评论
为什么被折叠?



