-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBridges_In_Graph.cpp
More file actions
43 lines (40 loc) · 944 Bytes
/
Bridges_In_Graph.cpp
File metadata and controls
43 lines (40 loc) · 944 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define ll long int
vector<int > adj[100005];
int vis[100005]={0};
ll in[100005], low[100005];
//finding bridges in a graph
ll timer=0;
void dfs(ll u, ll par){
vis[u]=1;
in[u]=low[u]=timer;
timer++;
for(auto child: adj[u]){
if(child==par)continue;
else if(vis[child]==1){
//back edge
low[u]=min(low[u],in[child]);
}
else{
dfs(child,u);
if(low[child]>in[u]){//means child can only be visited through long
//path that is from u and no other node
cout<<"Bridge found :- "<<u<<" -- "<<child<<endl;
}
low[u]=min(low[u],low[child]);
}
}
}
int main()
{
ll m,n,x,y;
cin>>m>>n;
for(int i=0;i<n;i++){
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1,-1);
return 0;
}