0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 12:58:48 +00:00

P1197 [JSOI2008] 星球大战

https://www.luogu.com.cn/record/77044997
This commit is contained in:
Baoshuo Ren 2022-06-07 09:00:52 +08:00
parent 49974f4d4a
commit e937f5acca
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

84
Luogu/P1197/P1197.cpp Normal file
View File

@ -0,0 +1,84 @@
#include <iostream>
#include <vector>
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<int> 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;
}