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);
}
}
9056



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



