-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathunion_find_bipartite.cpp
166 lines (131 loc) · 4.93 KB
/
union_find_bipartite.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
#include <vector>
#include <cassert>
#include <numeric>
struct union_find_bipartite {
struct node {
int parent, rank, size, status;
bool parent_edge_parity;
bool is_bipartite;
int ct_nodes_on_root_side;
node() {}
node(int id) : parent(id), rank(0), size(1), status(-1),
parent_edge_parity(0), is_bipartite(true), ct_nodes_on_root_side(1) {}
int count_bipartitions() const {
return !is_bipartite ? 0 : status == -1 ? 2 : 1;
}
int min_nodes_on_side_1() const {
switch (status) {
case 1: return ct_nodes_on_root_side;
case 0: return size - ct_nodes_on_root_side;
case -1: return std::min(ct_nodes_on_root_side, size - ct_nodes_on_root_side);
default: assert(false);
}
}
};
int ct_components, ct_bipartite_components, degrees_of_freedom, min_nodes_on_side_1;
mutable std::vector<node> data;
union_find_bipartite(int N = 0) : ct_components(N), ct_bipartite_components(N),
degrees_of_freedom(N), min_nodes_on_side_1(0), data(N) {
iota(data.begin(), data.end(), 0);
}
private:
void subtract_component(int u) {
ct_components--;
if (data[u].is_bipartite) {
ct_bipartite_components--;
degrees_of_freedom -= data[u].status == -1;
min_nodes_on_side_1 -= data[u].min_nodes_on_side_1();
}
}
void add_component(int u) {
ct_components++;
if (data[u].is_bipartite) {
ct_bipartite_components++;
degrees_of_freedom += data[u].status == -1;
min_nodes_on_side_1 += data[u].min_nodes_on_side_1();
}
}
public:
int find(int u) const {
if (u == data[u].parent) return u;
find(data[u].parent);
data[u].parent_edge_parity ^= data[data[u].parent].parent_edge_parity;
return data[u].parent = data[data[u].parent].parent;
}
bool can_constrain_node_to_side(int u, bool side) const {
find(u);
side ^= data[u].parent_edge_parity;
u = data[u].parent;
return data[u].is_bipartite &&
(data[u].status == -1 || data[u].status == side);
}
bool constrain_node_to_side(int u, bool side) {
find(u);
side ^= data[u].parent_edge_parity;
u = data[u].parent;
subtract_component(u);
if (data[u].status == -1) {
data[u].status = side;
} else {
data[u].is_bipartite &= data[u].status == side;
}
add_component(u);
return data[u].is_bipartite;
}
bool can_add_constraint_on_nodes(int u, int v, bool edge_parity) const {
find(u);
edge_parity ^= data[u].parent_edge_parity;
u = data[u].parent;
find(v);
edge_parity ^= data[v].parent_edge_parity;
v = data[v].parent;
return data[u].is_bipartite && data[v].is_bipartite &&
(data[u].status == -1 || data[v].status == -1 || (data[u].status ^ data[v].status) == edge_parity);
}
struct result {
bool added_connectivity;
bool component_is_bipartite;
};
result unite(int u, int v, bool edge_parity) {
find(u);
edge_parity ^= data[u].parent_edge_parity;
u = data[u].parent;
find(v);
edge_parity ^= data[v].parent_edge_parity;
v = data[v].parent;
if (u == v) {
subtract_component(u);
if (edge_parity)
data[u].is_bipartite = false;
add_component(u);
return {false, data[u].is_bipartite};
}
if (data[u].rank < data[v].rank)
std::swap(u, v);
subtract_component(u);
subtract_component(v);
data[v].parent = u;
data[v].parent_edge_parity = edge_parity;
if (data[u].rank == data[v].rank)
data[u].rank++;
data[u].size += data[v].size;
data[u].is_bipartite &= data[v].is_bipartite;
if (edge_parity)
data[u].ct_nodes_on_root_side += data[v].size - data[v].ct_nodes_on_root_side;
else
data[u].ct_nodes_on_root_side += data[v].ct_nodes_on_root_side;
if (data[v].status != -1) {
bool implied_u_status = data[v].status ^ edge_parity;
if (data[u].status == -1)
data[u].status = implied_u_status;
else
data[u].is_bipartite &= data[u].status == implied_u_status;
}
add_component(u);
return {true, data[u].is_bipartite};
}
bool can_constrain_to_be_same (int u, int v) const { return can_add_constraint_on_nodes(u, v, 0); }
bool can_constrain_to_be_different (int u, int v) const { return can_add_constraint_on_nodes(u, v, 1); }
result constrain_to_be_same (int u, int v) { return unite(u, v, 0); }
result constrain_to_be_different (int u, int v) { return unite(u, v, 1); }
};