标签: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; }
标签:tmp names turn printf else ota name root clu
原文地址:http://www.cnblogs.com/kprac/p/6979918.html