码迷,mamicode.com
首页 > 其他好文 > 详细

oj--poj2524:宗教信仰

时间:2017-06-10 22:41:45      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:tmp   names   turn   printf   else   ota   name   root   clu   

 

 待修改.

#include<cstdio>
#include<iostream>
#include<algorithm> //fill
using namespace std; 
int root[50005];
int casecnt=0;
int findroot(int x){
    if(root[x]==-1)
        return x;
    else{
        int tmp=findroot(root[x]);
        root[x]=tmp;
        return tmp;
    }
}
int main(){
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF){
        if(n==0&&m==0) break;
        casecnt++;
        fill(root,root+50005,-1);
        int tmp1,tmp2;
        for(int i=0;i<m;i++){
            scanf("%d %d",&tmp1,&tmp2);
            int roota=findroot(tmp1);
            int rootb=findroot(tmp2);
            if(roota!=rootb){
                root[roota]=rootb;
            }
        }
        int cnt=0;
        for(int i=1;i<=n;i++){
            printf("%d\n",root[i]);
            if(root[i]==-1)
                cnt++;
        }
        printf("Case %d: %d",casecnt,cnt);
    }
    return 0;
}

 

oj--poj2524:宗教信仰

标签:tmp   names   turn   printf   else   ota   name   root   clu   

原文地址:http://www.cnblogs.com/kprac/p/6979918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!