|
无向图找桥
| INIT: edge[][]
邻接矩阵
;vis[],pre[],anc[],bridge
置
0;
| CALL: dfs(0, -1, 1, n);
\*==================================================*/
int
bridge, edge[V][V], anc[V], pre[V], vis[V];
void
dfs(
int
cur,
int
father,
int
dep,
int
n)
{
// vertex: 0 ~ n-1
if
(bridge)
return
;
vis[cur] = 1; pre[cur] = anc[cur] = dep;
for
(
int
i=0; i<n; ++i)
if
(edge[cur][i]) {
if
(i != father && 1 == vis[i]) {
if
(pre[i] < anc[cur])
anc[cur] = pre[i];
//back edge
}
if
(0 == vis[i]) {
//tree edge
dfs(i, cur, dep+1, n);
if
(bridge)
return
;
if
(anc[i] < anc[cur]) anc[cur] = anc[i];
if
(anc[i] > pre[cur]) { bridge = 1;
return
; }
}
}
vis[cur] = 2;
}