#include<iostream> #define N 1005 #define FOR(i,s,t) for(int i=(s);i<=(t);i++) using namespace std; int n,m,sum; int father[N]; void make_set() { FOR(i,1,n) father[i]=i; sum=n; } int find_set(int a) { /*while(a!=father[a]) a=father[a]; return a; */ return a==father[a]?a:father[a]=find_set(father[a]); } void Union_set(int &a,int &b) { int x=find_set(a); int y=find_set(b); if(x!=y) { father[y]=x; sum--; } } int main() { while(cin>>n&&n) { cin>>m ; make_set(); FOR(i,1,m) { int a,b; cin>>a>>b; Union_set(a,b); } cout<<sum-1<<endl; } return 0; }
http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=791&pid=1001&ojid=0
最新推荐文章于 2024-08-17 14:22:19 发布