-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpartially_persistent_union_find.cpp
95 lines (84 loc) · 2.03 KB
/
partially_persistent_union_find.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
// added
#include <bits/stdc++.h>
using namespace std;
class PartiallyParsistentUnionFind {
vector<int> rank, par, time;
const int INF = 1e9;
public:
PartiallyParsistentUnionFind(int n) {
rank.resize(n);
par.resize(n, -1);
time.resize(n, INF);
}
int find(int t, int x) {
if (time[x] > t) return x;
return find(t, par[x]);
}
bool unite(int t, int x, int y) {
x = find(t, x);
y = find(t, y);
if (x == y) return false;
if (rank[x] > rank[y]) {
par[y] = x;
time[y] = t;
} else {
par[x] = y;
time[x] = t;
if (rank[x] == rank[y]) {
rank[y]++;
}
}
return true;
}
bool same(int t, int x, int y) {
return find(t, x) == find(t, y);
}
};
int main() {
int n, m, q;
cin >> n >> m >> q;
using ii = pair<int, int>;
map<ii, int> ord;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
ord[ii(a, b)] = 1e9;
}
vector<ii> qs;
for (int i = 0; i < q; i++) {
int c, d;
cin >> c >> d;
c--, d--;
qs.emplace_back(c, d);
}
reverse(qs.begin(), qs.end());
for (int i = 0; i < q; i++) {
ord[qs[i]] = i + 1;
}
PartiallyParsistentUnionFind uf(n);
vector<pair<int, ii>> vs;
for (auto &p: ord) {
if (p.second == 1e9) {
uf.unite(0, p.first.first, p.first.second);
} else {
vs.emplace_back(p.second, p.first);
}
}
sort(vs.begin(), vs.end());
for (auto &v: vs) {
uf.unite(v.first, v.second.first, v.second.second);
}
for (int i = 1; i < n; i++) {
int l = -1, r = q + 1;
while (l < r - 1) {
int mid = (l + r) / 2;
if (uf.same(mid, 0, i)) {
r = mid;
} else {
l = mid;
}
}
cout << (l == -1 ? -1 : (l == q ? 0 : q - l)) << endl;
}
}