From 51a98ba4b7abc5bd025c0384c66859208414a0ae Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Mon, 19 Oct 2020 22:25:55 +0800 Subject: [PATCH] =?UTF-8?q?P2097=20=E8=B5=84=E6=96=99=E5=88=86=E5=8F=911?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R40129919 --- problem/P2097/P2097.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 problem/P2097/P2097.cpp diff --git a/problem/P2097/P2097.cpp b/problem/P2097/P2097.cpp new file mode 100644 index 00000000..1eafd04a --- /dev/null +++ b/problem/P2097/P2097.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +vector l[100010]; +int n, m, f, s, ans, pd[100010]; + +void dfs(int x) { + for (int y = 0; y < l[x].size(); y++) { + if (!pd[l[x][y]]) { + pd[l[x][y]] = 1; + dfs(l[x][y]); + } + } +} + +int main() { + int p, q; + cin >> n >> m; + for (int i = 1; i <= m; i++) { + cin >> p >> q; + l[p].push_back(q); + l[q].push_back(p); + } + for (int i = 1; i <= n; i++) { + if (!pd[i]) { + ans++; + pd[i] = 1; + dfs(i); + } + } + cout << ans << endl; + return 0; +}