-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1677.cpp
67 lines (62 loc) · 1.26 KB
/
1677.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, k, par[N], cntcmp;
bool mark[N];
vector<pair<int, int>> down;
vector<tuple<int, int, int>> track;
map<pair<int, int>, int> mp;
int root(int v) {
return (par[v] < 0) ? v : root(par[v]);
}
void join(int x, int y) {
if ((x = root(x)) == (y = root(y))) {
track.emplace_back(0, 0, 0);
return;
}
if (par[x] > par[y]) swap(x, y);
track.emplace_back(x, y, par[y]);
par[x] += par[y];
par[y] = x;
--cntcmp;
}
void rollback() {
auto [x, y, t] = track.back();
track.pop_back();
if (x) {
par[x] -= t;
par[y] = t;
++cntcmp;
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> k;
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
if (u > v) swap(u, v);
mp[{u, v}] = i;
}
for (int i = 1, u, v; i <= k; ++i) {
cin >> u >> v;
if (u > v) swap(u, v);
down.emplace_back(u, v);
mark[mp[{u, v}]] = true;
}
reverse(down.begin(), down.end());
fill(par + 1, par + 1 + n, -1);
cntcmp = n;
for (auto [e, i]: mp) {
if (!mark[i]) {
join(e.first, e.second);
}
}
for (auto [u, v]: down) {
join(u, v);
}
while (k--) {
rollback();
cout << cntcmp << "\n";
}
return 0;
}