文章
34
粉丝
89
获赞
2
访问
19.2k
#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; } }
登录后发布评论
暂无评论,来抢沙发