From e937f5acca6ab54d27c35d384e3d8064db75d8d9 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 7 Jun 2022 09:00:52 +0800 Subject: [PATCH] =?UTF-8?q?P1197=20[JSOI2008]=20=E6=98=9F=E7=90=83?= =?UTF-8?q?=E5=A4=A7=E6=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77044997 --- Luogu/P1197/P1197.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Luogu/P1197/P1197.cpp diff --git a/Luogu/P1197/P1197.cpp b/Luogu/P1197/P1197.cpp new file mode 100644 index 00000000..160836b9 --- /dev/null +++ b/Luogu/P1197/P1197.cpp @@ -0,0 +1,84 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 4e5 + 5; + +int n, m, k, a[N], fa[N], ans[N]; +bool vis[N], del[N]; +std::vector g[N]; + +int find(int x) { + return fa[x] == x ? x : fa[x] = find(fa[x]); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 0, x, y; i < m; i++) { + cin >> x >> y; + + g[x].push_back(y); + g[y].push_back(x); + } + + for (int i = 0; i < n; i++) fa[i] = i; + + cin >> k; + + for (int i = 1; i <= k; i++) { + cin >> a[i]; + + del[a[i]] = true; + } + + for (int i = 0; i < n; i++) { + if (!del[i]) { + for (int v : g[i]) { + if (!del[v]) { + int f1 = find(i), + f2 = find(v); + + fa[f1] = f2; + } + } + } + } + + int cnt = 0; + for (int i = 0; i < n; i++) { + if (fa[i] == i && !del[i]) cnt++; + } + ans[k] = cnt; + + for (int i = k; i; i--) { + del[a[i]] = false; + cnt++; + + for (int v : g[a[i]]) { + if (!del[v]) { + int f1 = find(a[i]), + f2 = find(v); + + if (f1 != f2) { + fa[f1] = f2; + cnt--; + } + } + } + + ans[i - 1] = cnt; + } + + for (int i = 0; i <= k; i++) { + cout << ans[i] << endl; + } + + return 0; +}