1
0
Fork 0

Add graph/examples/tarjan-bridge.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-10-12 21:30:52 +08:00
parent ce5a34287d
commit 92df85050d
1 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,25 @@
vector<bool> isbridge(m);
{
vector<int> low(n + 1), dfn(n + 1), father(n + 1);
int dfs_clock = 0;
int cnt_bridge = 0;
auto dfs = [&] (auto dfs, int u, int fa) -> void {
father[u] = fa;
low[u] = dfn[u] = ++dfs_clock;
for (auto&& [v, i] : ch[u]) {
if (!dfn[v]) {
dfs(dfs, v, u);
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) {
isbridge[i] = true;
++cnt_bridge;
}
} else if (dfn[v] < dfn[u] && v != fa) {
low[u] = min(low[u], dfn[v]);
}
}
};
for (int i = 1; i <= n; ++i) {
if (not dfn[i]) dfs(dfs, i, 0);
}
}