kdplus @ 2024-10-10 23:51:43
有一种可能是你也和我一样误解了题意,虽然他写的是攻占了几乎所有的星球,但是其实他想说,一开始的时候 0 - n-1的星球全是反抗军的了。 而我以外只有m条边提到的星球才是反抗军初始的星球。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
class dsu {
public:
vector<int> data;
int n, cnt;
dsu(int _n) : n(_n), cnt(_n) {
data.assign(n, -1);
}
inline int find(int x) {
return (data[x] < 0 ? x : data[x] = find(data[x]));
}
inline int size(int x) {
return -data[find(x)];
}
inline bool same(int x, int y) {
return find(x) == find(y);
}
inline bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return false;
if (data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
--cnt;
return true;
}
};
void solve() {
int n, m;
cin >> n >> m;
dsu d(n);
vector E(n, vector<ll>(0));
vector<pair<ll, ll>> es;
unordered_set<ll> remove, have;
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
es.push_back({x, y});
E[x].push_back(y), E[y].push_back(x);
have.insert(x), have.insert(y);
}
int k;
cin >> k;
vector<ll> v(k);
for (int i = 0; i < k; ++i) {
cin >> v[i];
remove.insert(v[i]);
have.erase(v[i]);
}
for (auto &[x, y] : es)
if (!remove.count(x) and !remove.count(y)) d.unite(x, y);
reverse(v.begin(), v.end());
vector<ll> ans;
ll cnt = d.cnt - k, cnt2 = 0;
for (auto &h : have)
if (d.data[h] < 0) ++cnt2;
assert(cnt2 == cnt);
ans.push_back(cnt);
for (auto &x : v) {
if (!have.count(x)) ++cnt;
have.insert(x);
for (auto &y : E[x])
if (have.count(y)) cnt -= d.unite(x, y);
ans.push_back(cnt);
}
reverse(ans.begin(), ans.end());
for (auto &a : ans) cout << a << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
solve();
return 0;
}
by kdplus @ 2024-10-11 00:02:53
s/以外/以为