-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbipartite_check_online.cpp
83 lines (72 loc) · 1.59 KB
/
bipartite_check_online.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
// pii in parent means <leader index, parity of path up to its leader>
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
vector<pii> parent;
vector<int> depth;
vector<int> bipartite;
void build(int n) {
parent.resize(n);
depth.resize(n);
bipartite.assign(n, 1);
for (int i = 0; i < n; i++)
parent[i] = {i, 0};
}
void make_set(int v) {
parent[v] = {v, 0};
depth[v] = 0;
bipartite[v] = 1;
}
pii find_set(int v) {
if (v != parent[v].first) {
int parity = parent[v].second;
parent[v] = find_set(parent[v].first);
parent[v].second ^= parity;
}
return parent[v];
}
void add_edge(int a, int b) {
pii pa = find_set(a);
a = pa.first;
int x = pa.second;
pii pb = find_set(b);
b = pb.first;
int y = pb.second;
if (a == b) {
if (x == y)
bipartite[a] = false;
} else {
if (depth[a] < depth[b])
swap(a, b);
parent[b] = {a, x^y^1};
bipartite[a] &= bipartite[b];
if (depth[a] == depth[b])
depth[a]++;
}
}
bool is_bipartite(int v) {
return bipartite[find_set(v).first];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n = 5;
build(n);
// 0 - 1
// \2/
add_edge(0, 1);
cout << is_bipartite(0) << '\n';
add_edge(1, 2);
cout << is_bipartite(1) << '\n';
add_edge(0, 2);
cout << is_bipartite(0) << '\n';
cout << is_bipartite(1) << '\n';
cout << is_bipartite(2) << '\n';
// 0 - 1 - 3 - 4
// \2/
add_edge(3, 4);
cout << is_bipartite(3) << '\n';
add_edge(1, 3);
cout << is_bipartite(3) << '\n';
return 0;
}