-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBridges_online.cpp
148 lines (113 loc) · 2.33 KB
/
Bridges_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
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
#include <iostream>
#include <vector>
typedef long long ll;
using namespace std;
vector<int> bcomp; // dsu for doubly connected components
vector<int> comp; // dsu for conectivity components
vector<int> sz, par, used;
int bridges;
int get_bcomp(int u) {
if (u == -1) return -1;
if (bcomp[u] == u)
return u;
return bcomp[u] = get_bcomp(bcomp[u]);
}
int get_comp(int u) {
u = get_bcomp(u);
if (comp[u] == u)
return u;
return comp[u] = get_comp(comp[u]);
}
void rehand_tree(int u) {
u = get_bcomp(u);
int it = u;
int child = -1;
while (it != -1) {
int t = get_bcomp(par[it]);
par[it] = child;
comp[it] = u;
child = it; it = t;
}
sz[u] = sz[child];
}
int u_cnt;
int comp_cycle(int a, int b) {
vector <int> path_a, path_b;
u_cnt++;
int lca;
while (true) {
if (a != -1) {
a = get_bcomp(a);
path_a.push_back(a);
if (used[a] == u_cnt) {
lca = a;
break;
}
used[a] = u_cnt;
a = par[a];
}
if (b != -1) {
b = get_bcomp(b);
path_b.push_back(b);
if (used[b] == u_cnt) {
lca = b;
break;
}
used[b] = u_cnt;
b = par[b];
}
}
int cnt = 0;
for (int i = 0; i < path_a.size(); i++) {
bcomp[path_a[i]] = lca;
if (path_a[i] == lca) break;
cnt++;
}
for (int i = 0; i < path_b.size(); i++) {
bcomp[path_b[i]] = lca;
if (path_b[i] == lca) break;
cnt++;
}
return cnt;
}
void add_edge(int a, int b) {
a = get_bcomp(a); b = get_bcomp(b);
int comp_a = get_comp(a), comp_b = get_comp(b);
if (comp_a != comp_b) {
bridges++;
if (sz[comp_a] > sz[comp_b]) {
swap(comp_a, comp_b);
swap(a, b);
}
rehand_tree(a);
par[a] = b;
comp[a] = b;
sz[comp_b] += sz[a];
}
else {
if (a != b)
bridges -= comp_cycle(a, b);
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << "Enter vertex and edges number:\n";
//graph vertex from 0 to n-1
int n, m;
cin >> n >> m;
bcomp.assign(n, 0);
comp.assign(n, 0);
sz.assign(n, 1);
par.assign(n, -1);
used.assign(n, 0);
for (int i = 0; i < n; i++)
bcomp[i] = comp[i] = i;
cout << "Enter edges:\n";
int x, y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
add_edge(x, y);
cout << "number of bridges: " << bridges << '\n';
}
return 0;
}