0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-25 06:11:59 +00:00

P1536 村村通

R43106178
This commit is contained in:
Baoshuo Ren 2020-12-03 17:38:27 +08:00 committed by Baoshuo Ren
parent 6ea0b00856
commit 87f9e1d85d
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

33
problem/P1536/P1536.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, ans, f[100005];
int find(int x) {
if (f[x] == x) return x;
return f[x] = find(f[x]);
}
void unity(int x, int y) {
f[find(x)] = find(y);
}
int main() {
while (cin >> n, n) {
ans = 0;
cin >> m;
for (int i = 1; i <= n; i++) {
f[i] = i;
}
for (int i = 1; i <= m; i++) {
cin >> x >> y;
unity(x, y);
}
for (int i = 1; i <= n; i++) {
if (find(i) == i) ans++;
}
cout << --ans << endl;
}
return 0;
}