forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2133 - Dynamic Connectivity.cpp
More file actions
127 lines (111 loc) · 2.57 KB
/
2133 - Dynamic Connectivity.cpp
File metadata and controls
127 lines (111 loc) · 2.57 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Dynamic Connectivity
//
// Problem name: Dynamic Connectivity
// Problem Link: https://cses.fi/problemset/task/2133
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int INF = 2e9;
// Seg
struct no {
vector<pii> v;
no() {
v = vector<pii> ();
}
} a[4 * MAXN];
void update(int node, int i, int j, int ini, int fim, pii val) {
if (j < ini || i > fim) return;
else if (ini <= i && j <= fim) {
a[node].v.push_back(val);
}
else {
int m = (i + j) / 2;
update(2 * node, i, m, ini, fim, val);
update(2 * node + 1, m + 1, j, ini, fim, val);
}
}
int ans[MAXN], pai[MAXN], sz[MAXN], resp;
map<pii, int> mp;
stack<int> st;
// DSU
int find(int x) {
if (x == pai[x]) return x;
return find(pai[x]);
}
void join(int a, int b) {
a = find(a), b = find(b);
if (sz[a] < sz[b]) {
swap(a, b);
}
pai[b] = a;
sz[a] += sz[b];
st.push(b);
resp--;
}
void rollback() {
int at = st.top();
st.pop();
sz[pai[at]] -= sz[at];
pai[at] = at;
resp++;
}
//
void dfs(int node, int i, int j) {
int cnt = 0;
for (auto x : a[node].v) {
if (find(x.first) != find(x.second)) {
join(x.first, x.second);
cnt++;
}
}
if (i == j) {
ans[i] = resp;
}
else {
int m = (i + j) / 2;
dfs(2 * node, i, m);
dfs(2 * node + 1, m + 1, j);
}
for (int i = 0; i < cnt; i++) {
rollback();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k, tipo, a, b;
cin >> n >> m >> k;
resp = n;
for (int i = 1; i <= n; i++) pai[i] = i, sz[i] = 1;
for (int i = 0; i < m; i++) {
cin >> a >> b;
if (a > b) swap(a, b);
mp[{a, b}] = 0;
}
for (int i = 1; i <= k; i++) {
// tempo i
cin >> tipo >> a >> b;
if (a > b) swap(a, b);
if (tipo == 1) {
mp[{a, b}] = i;
}
else {
update(1, 0, k + 1, mp[{a, b}], i - 1, {a, b});
mp[{a, b}] = -1;
}
}
for (auto x : mp) {
if (x.second == -1) continue;
update(1, 0, k + 1, x.second, k + 1, x.first);
}
dfs(1, 0, k + 1);
for (int i = 0; i <= k; i++) cout << ans[i] << ' ';
cout << '\n';
return 0;
}