-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridges.cpp
33 lines (33 loc) · 853 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
template<typename T>
struct bridge{
vector<vector<T>> edge;
vector<bool> vis;
vector<T> tin;
vector<T> low;
T timer=0;
T n;
vector<pair<T,T>> ans;// get ans
bridge(T node,vector<vector<T>> e,T start){// no of nodes , edge , startting node
edge=e;
n=node;
vis=vector<bool>(n);
tin=vector<T>(n);
low=vector<T>(n);
dfs(start,-1);
}
void dfs(int v, int p) {
vis[v] = true;
tin[v] = low[v] = timer++;
for (int to : edge[v]) {
if (to == p) continue;
if (vis[to]) {
low[v] = min(low[v], tin[to]);
} else {
dfs(to, v);
low[v] = min(low[v], low[to]);
if (low[to] > tin[v])
ans.push_back({v, to});
}
}
}
};