From 87f9e1d85decfbcf98cfefcc97d2cfccf18bffda Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 3 Dec 2020 17:38:27 +0800 Subject: [PATCH] =?UTF-8?q?P1536=20=E6=9D=91=E6=9D=91=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R43106178 --- problem/P1536/P1536.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 problem/P1536/P1536.cpp diff --git a/problem/P1536/P1536.cpp b/problem/P1536/P1536.cpp new file mode 100644 index 00000000..83c9bd84 --- /dev/null +++ b/problem/P1536/P1536.cpp @@ -0,0 +1,33 @@ +#include + +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; +}