-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathunion_find_can_undo.cpp
221 lines (188 loc) · 5.35 KB
/
union_find_can_undo.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// undo つき Union-Find tree
//
// verified:
// AtCoder ABC 302 Ex - Ball Collector
// https://atcoder.jp/contests/abc302/tasks/abc302_h
//
// Codeforces Round 680 (Div.1) C. Team-Building
// https://codeforces.com/contest/1444/problem/C
//
#include <bits/stdc++.h>
using namespace std;
// Union-Find, we can undo
struct UnionFind {
// core member
vector<int> par;
stack<pair<int,int>> history;
// constructor
UnionFind() {}
UnionFind(int n) : par(n, -1) { }
void init(int n) { par.assign(n, -1); }
// core methods
int root(int x) {
if (par[x] < 0) return x;
else return root(par[x]);
}
bool same(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y) {
x = root(x), y = root(y);
history.emplace(x, par[x]);
history.emplace(y, par[y]);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) {
return -par[root(x)];
}
// 1-step undo
void undo() {
for (int iter = 0; iter < 2; ++iter) {
par[history.top().first] = history.top().second;
history.pop();
}
}
// erase history
void snapshot() {
while (!history.empty()) history.pop();
}
// all rollback
void rollback() {
while (!history.empty()) undo();
}
// debug
friend ostream& operator << (ostream &s, UnionFind uf) {
map<int, vector<int>> groups;
for (int i = 0; i < uf.par.size(); ++i) {
int r = uf.root(i);
groups[r].push_back(i);
}
for (const auto &it : groups) {
s << "group: ";
for (auto v : it.second) s << v << " ";
s << endl;
}
return s;
}
};
//------------------------------//
// Examples
//------------------------------//
void ABC_302_Ex() {
using pint = pair<int,int>;
using Graph = vector<vector<int>>;
int N;
cin >> N;
vector<int> A(N), B(N);
for (int i = 0; i < N; ++i) {
cin >> A[i] >> B[i];
--A[i], --B[i];
}
Graph G(N);
for (int i = 0; i < N-1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
G[u].push_back(v);
G[v].push_back(u);
}
// Union-Find
// 外部データの undo もここで実現する
UnionFind uf(N);
vector<int> nums(N, 0); // 各連結成分ごとの種類数
int cur = 0; // 現時点での種類数の最大値
vector<pair<pint,int>> hist; // 履歴
vector<int> res(N, 0); // 答え
auto insert = [&](int v, int p) -> void {
int x = uf.root(A[v]), y = uf.root(B[v]);
hist.push_back(make_pair(pint(x, nums[x]), cur));
hist.push_back(make_pair(pint(y, nums[y]), cur));
int before = (uf.same(x, y) ? nums[x] : nums[x] + nums[y]);
int after = (uf.same(x, y) ?
min(uf.size(x), nums[x] + 1) :
min(uf.size(x) + uf.size(y), nums[x] + nums[y] + 1));
uf.merge(x, y);
nums[uf.root(x)] = after;
res[v] = (p != -1 ? res[p] : 0) + (after - before);
};
auto erase = [&]() -> void {
for (int iter = 0; iter < 2; ++iter) {
nums[hist.back().first.first] = hist.back().first.second;
cur = hist.back().second;
hist.pop_back();
}
uf.undo();
};
// DFS
auto dfs = [&](auto self, int v, int p) -> void {
insert(v, p);
for (auto v2 : G[v]) {
if (v2 == p) continue;
self(self, v2, v);
}
erase();
};
dfs(dfs, 0, -1);
for (int v = 1; v < N; ++v) cout << res[v] << " ";
cout << endl;
}
void CF_680_DIV1_C() {
using pint = pair<int,int>;
using Graph = vector<vector<int>>;
cin.tie(0);
ios::sync_with_stdio(false);
// 入力
int N, M, K;
cin >> N >> M >> K;
vector<int> C(N);
for (int i = 0; i < N; ++i) cin >> C[i], --C[i];
Graph G(N);
UnionFind uf(N*2);
for (int i = 0; i < M; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
G[u].push_back(v), G[v].push_back(u);
if (C[u] == C[v]) uf.merge(u, v+N), uf.merge(u+N, v);
}
// これまでの履歴の削除
uf.snapshot();
// すでに二部グラフはダメ
vector<bool> isbi(K, true);
long long con = K;
for (int v = 0; v < N; ++v) {
if (!isbi[C[v]]) continue;
if (uf.same(v, v+N)) --con, isbi[C[v]] = false;
}
long long res = con * (con - 1) / 2;
// 辺の色を分類
map<pint, vector<pint>> ma;
for (int v = 0; v < N; ++v) {
for (auto u : G[v]) {
if (!isbi[C[v]] || !isbi[C[u]] || C[v] == C[u]) continue;
ma[pint(min(C[v], C[u]), max(C[v], C[u]))].push_back(pint(v, u));
}
}
// 各ペアごとに追加していく
for (auto it : ma) {
bool ok = true;
for (auto e : it.second) {
int u = e.first, v = e.second;
uf.merge(u, v+N), uf.merge(u+N, v);
if (uf.same(u, u+N) || uf.same(v, v+N)) ok = false;
}
if (!ok) --res;
// 元に戻す
uf.rollback();
}
cout << res << endl;
}
int main() {
ABC_302_Ex();
//CF_680_DIV1_C();
}