-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridges.cpp
52 lines (44 loc) · 1.05 KB
/
bridges.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
#include<bits/stc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void dfs(int node, int parent, vector<int> &vis, vector<int> &tin, vector<int> &low, int &timer, vector<int> adj[]){
vis[node]=1;
tin[node]=low[node]=timer++;
for(auto it:adj[node]){
if(it==parent)
continue;
if(!vis[it]){
dfs(it, node, vis, tin, low, timer, adj);
low[node]=min(low[node], low[it]);
if(low[it]> tin[node]){
cout<<node<<" "<<it<<endl;
}
}
else{
low[node]=min(low[node], tin[it]);
}
}
}
int main(){
int n,m;
cin>>n>>m;
vector<int> adj[n];
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> vis(n,0);
vector<int> tin(n,-1);
vector<int> low(n,-1);
int timer=0;
for(int i=0;i<n;i++){
if(!vis[i]){
dfs(i, -1, vis, tin, low, timer, adj);
}
}
return 0;
}