forked from t3nsor/codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridges.cpp
43 lines (40 loc) · 1009 Bytes
/
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
const int N = 1e5 + 10;
vector<int> adj[N];
bool visited[N] = {0};
vector<pll> bridges;
vector<int> cutPts;
// Use p = -1 for root
void dfs(int u, int p = -1, int d = 0){
visited[u] = true;
dep[u] = d;
fup[u] = d;
bool isCutpt = false;
for(int v : adj[u]){
if(v == p)
continue;
if(not visited[v]){
dfs(v, u, d+1);
fup[u] = min( fup[u] , fup[v] );
//Cut-Vertices
if(fup[v] >= dep[u]){
if(p != -1){ //Excluding root
isCutpt = true;
}
else if(adj[u].size() > 1){ //For root
isCutpt = true;
}
}
//Bridges
if(fup[v] > dep[u]){
bridges.pb(mp(u,v));
}
}
else{
fup[u] = min(fup[u], dep[v]);
}
}
//Cut - Vertices
if(isCutpt){
cutPts.pb(u);
}
}