文章
99
粉丝
120
获赞
8
访问
96.8k
#include <iostream>
#include <cstdio>
using namespace std;
int fa[1005];
int find(int x){
if(x == fa[x]){
return x;
}
return fa[x] = find(fa[x]);
}
int main() {
int n,m;
while(cin >> n >> m){
if(n == 0){
break;
}
for(int i = 1;i <= n;i++){
fa[i] = i;
}
for(int i = 1;i <= m;i++){
int j,k;
cin >> j >> k;
fa[find(k)] = find(j);
}
int sum = 0;
for(int i = 1;i <= n;i++){
if(fa[i] == i){
sum++;
}
}
cout << sum - 1 << endl;
}
}
登录后发布评论
暂无评论,来抢沙发