-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbridge_finding.cpp
72 lines (59 loc) · 1.48 KB
/
bridge_finding.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
// Vivek Rai
// Blazer_007
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
typedef long long int ll;
const int hell = 1e9 + 7 ;
const int N = (int)1e5 + 5;
vector<int> graph[ N ];
vector<bool> visited( N , false );
vector<int> in( N , 0 ); // in[i] -> entry time of ith node
vector<int> low( N , 0); // low[i] -> lowest ancestor that can be reached from ith node
int timer;
void dfs(int src, int par)
{
visited[src] = true;
in[src] = low[src] = timer;
timer++;
for(int node : graph[src])
{
if (node == par)
continue;
else if (visited[node] == true)
low[src] = min(in[node], low[src]);
else
{
dfs(node, src);
if (low[node] > in[src])
cout << src << " -> " << node << " bridge" << endl;
low[src] = min(low[src], low[node]);
}
}
}
signed main()
{
fastio
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
while(m--)
{
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for(int i = 0 ; i < N ; i++)
visited[i] = false;
timer = 0;
for (int i = 1; i <= n; ++i) {
if (!visited[i])
dfs(i, -1);
}
return 0;
}