-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBridges.cpp
More file actions
92 lines (87 loc) · 1.48 KB
/
Bridges.cpp
File metadata and controls
92 lines (87 loc) · 1.48 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1000];
int in[1000],low[1000],vis[1000];
set<int> ss;
int timer=0;
void find_articpts(int cur,int par)
{
vis[cur]=1;
in[cur]=timer;
low[cur]=timer;
timer++;
int children=0;
for(auto nbr:adj[cur])
{
if(nbr==par)
continue;
if(vis[nbr])
{
//back edge
low[cur]=min(low[cur],in[nbr]);
}
else
{
dfs(nbr,cur);
if(low[nbr]>=in[cur] and par!=-1)
{
//bridge
ss.insert(cur);
cout<<nbr<<"-->"<<cur<<" this is a bridge\n";
}
children++;
low[cur]=min(low[cur],low[nbr]);
}
}
if(children>1 and par==-1)
{
//parent should have more than 1 child for it being an articulation point
ss.insert(cur);
}
}
vector<int> adj[100001];
vector<vector<int>> ans;
int discovery[100001];
int low[100001];
int timer=0;
void find_bridges(int src,int par)
{
discovery[src]=timer;
low[src]=timer;
timer+=1;
for(auto x:adj[src])
{
if(x==par)
continue;
if(discovery[x]!=-1)
{
//backedge
low[src]=min(low[src],discovery[x]);
}
else
{
dfs(x,src);
if(low[x]>discovery[src])
{
ans.push_back({src,x});
}
low[src]=min(low[src],low[x]);
}
}
return;
}
int main()
{
int n,m;
cin>>n>>m;
while(m--)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
dfs(1,-1);
cout<<ss.size()<<endl;
}
return 0;
}