forked from saketh-are/algo-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union_find_rewindable.cpp
152 lines (122 loc) · 4.09 KB
/
union_find_rewindable.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
#include <vector>
#include <numeric>
#include <cassert>
// {{{ data_structures/splitmix64_hash_map }}}
struct union_find_rewindable {
struct node {
int parent, rank, size;
node (int id = 0) : parent(id), rank(0), size(1) {}
};
struct modification {
int parent, child;
node previous_value;
};
std::vector<node> data;
std::vector<modification> history;
union_find_rewindable(int SZ = 0) : data(SZ) {
iota(data.begin(), data.end(), 0);
}
// Returns the root of the component containing i
int find(int i) const {
while (i != data[i].parent)
i = data[i].parent;
return i;
}
bool is_root(int i) const {
return i == find(i);
}
const node& root_node(int i) const {
return data[find(i)];
}
/* Unites the components containing a and b if they are different.
* Returns a boolean indicating whether a and b were in different components.
*/
bool unite(int a, int b) {
a = find(a), b = find(b);
if(a == b) return false;
if (data[a].rank < data[b].rank)
std::swap(a, b);
history.push_back({ a, b, data[a] });
data[b].parent = a;
data[a].size += data[b].size;
if (data[a].rank == data[b].rank)
data[a].rank++;
return true;
}
int component_count() const {
return int(data.size() - history.size());
}
int version() const {
return int(history.size());
}
void rewind(int version_id) {
assert(0 <= version_id && version_id <= int(history.size()));
while (int(history.size()) > version_id) {
auto [parent, child, previous_value] = history.back();
data[parent] = previous_value;
data[child].parent = child;
history.pop_back();
}
}
};
/* Processes a sequence of events (u, v) toggling undirected edges
* in a graph on vertex_count vertices. If an edge between u and v
* is present, it's removed; otherwise, one is added.
*
* Returns f[x] = the number of components in the graph after
* processing the first x events.
*/
std::vector<int> get_component_counts(int vertex_count,
std::vector<std::pair<int, int>> edge_sequence) {
const int E = int(edge_sequence.size());
std::vector<int> matching_operation(E, E);
__gnu_pbds::gp_hash_table<std::pair<int, int>, int,
sp64_pair_hash<int>> inserted_by;
for (int i = 0; i < E; i++) {
auto it = inserted_by.find(edge_sequence[i]);
if (it == inserted_by.end()) {
inserted_by[edge_sequence[i]] = i;
} else {
matching_operation[it->second] = i;
matching_operation[i] = it->second;
inserted_by.erase(edge_sequence[i]);
}
}
union_find_rewindable uf(vertex_count);
std::vector<int> component_counts(E + 1);
component_counts[0] = uf.component_count();
auto solve = [&](auto& self, int first, int last) -> void {
if (last - first == 1) {
component_counts[last] = uf.component_count();
if (matching_operation[first] > first) {
auto [u, v] = edge_sequence[first];
component_counts[last] -= uf.find(u) != uf.find(v);
}
return;
}
const int mid = first + (last - first) / 2;
const int initial_state = uf.version();
if (first < mid) {
for (int i = mid; i < last; i++) {
if (matching_operation[i] < first) {
auto [u, v] = edge_sequence[i];
uf.unite(u, v);
}
}
self(self, first, mid);
uf.rewind(initial_state);
}
if (mid < last) {
for (int i = first; i < mid; i++) {
if (matching_operation[i] >= last) {
auto [u, v] = edge_sequence[i];
uf.unite(u, v);
}
}
self(self, mid, last);
uf.rewind(initial_state);
}
};
solve(solve, 0, E);
return component_counts;
}