-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbridges_in_graph.cpp
64 lines (56 loc) · 1.48 KB
/
bridges_in_graph.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// TC: O(E+V)
///////////////////// start yanking ////////////////////
vector<vector<ll>> graph;
vector<bool> visited;
vector<ll> disc, low;
vector<pair<ll, ll>> bridges;
ll timer;
void build(ll n) { // call me (precomputation)
graph.resize(n+1);
visited.resize(n+1);
disc.resize(n+1), low.resize(n+1);
}
void insert_edge(ll s, ll d) { // call me (building)
graph[s].push_back(d);
graph[d].push_back(s); // remove if directed
}
void dfs(ll start, ll p = -1) {
visited[start] = true, disc[start] = low[start] = ++timer;
for (auto &i : graph[start]) {
if (!visited[i]) {
dfs(i, start);
low[start] = min(low[start], low[i]);
if (low[i] > disc[start]) {
bridges.push_back({start, i});
}
} else if (i != p) {
low[start] = min(low[start], disc[i]);
}
}
}
void find_bridges() { // call me (result)
for (ll i = 1; i < graph.size(); i++) {
if (!visited[i]) {
dfs(i);
}
}
}
///////////////////// stop yanking /////////////////////
int main() {
ll v, e, s, d;
cin >> v;
build(v); // called
cin >> e;
for (ll i = 0; i < e; i++) {
cin >> s >> d;
insert_edge(s, d); // called
}
find_bridges(); // called
for (ll i = 0; i < bridges.size(); i++) {
cout << bridges[i].first << " " << bridges[i].second << "\n";
}
return 0;
}