-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBridge searching algorithm.cpp
67 lines (61 loc) · 1.11 KB
/
Bridge searching algorithm.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
// Bridge searching algorithm - (using dfs tree)
// 'By Anki'
#include<bits/stdc++.h>
// #include<iostream>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define wr(i) cout<<#i<<" = "<<i<<", ";
#define wre(i) cout<<#i<<" = "<<i<<endl;
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000007LL
typedef unsigned long long ull;
#define M 200005
using u64= uint64_t;
vector<ll>v[M];
vector<bool>vis(M);
vector<ll>in(M),low(M);
vector<pair<ll,ll>>bridge;
ll timer=0LL;
void dfs(ll n,ll pr){
vis[n]=true;
in[n]=low[n]=timer++;
for(auto x:v[n]){
if(x==pr)continue;
if(vis[x])low[n]=min(in[x],low[n]);
else
{
dfs(x,n);
low[n]=min(low[n],low[x]);
if(low[x]>in[n]){
bridge.pb(mp(n,x));
}
}
}
}
int main()
{
fast
ll t=1LL;
// cin>>t;
while(t--)
{
ll n,m;
cin>>n>>m;
ll x,y;
in.assign(n+1,-1);
low.assign(n+1,-1);
for(int i=0;i<m;i++)
{
cin>>x>>y;
v[x].pb(y);
v[y].pb(x);
}
dfs(0,-1);
for(auto x:bridge){
cout<<x.first<<" - "<<x.second<<"\n";
}
}
}