标签:des blog io ar os sp java for strong
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<string> #include<algorithm> #include<queue> #include<vector> int set[1010],n,m,ans; int find(int x) { return x==set[x]?x:set[x]=find(set[x]); } void Union(int x,int y) { int ux,uy; ux=find(x),uy=find(y); if(ux!=uy) set[ux]=uy; } int main() { while(scanf("%d",&n),n) { ans=0; scanf("%d",&m); for(int i=1;i<=n;i++) set[i]=i; for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); Union(x,y); } for(int i=1;i<=n;i++) if(set[i]==i) ans++; printf("%d\n",ans-1); } return 0; }
标签:des blog io ar os sp java for strong
原文地址:http://www.cnblogs.com/a972290869/p/4101074.html