求解图的割点与桥
割点与桥详解地址
时间复杂度\(|E| + |V|\)
板子
int edge[MAXN][MAXN];
int bridge[MAXN][MAXN], cut[MAXN];
int low[MAXN], dfn[MAXN], vis[MAXN];
int ans;
void cut_bridge(int cur, int father, int dep, int n) {
vis[cur] = 1;
dfn[cur] = low[cur] = dep;
int children = 0;
for (int i = 0; i < n; i++) {
if (edge[cur][i]) {
if (i != father && 1 == vis[i]) {
if (dfn[i] < low[cur]) {
low[cur] = dfn[i];
}
} else if (0 == vis[i]) {
cut_bridge(i, cur, dep+1, n);
children++;
if (low[i] < low[cur]) {
low[cur] = low[i];
}
if ((father == -1 && children > 1) || (father != -1 && low[i] >= dfn[cur])) {
cut[cur] = true;
}
if (low[i] > dfn[cur]) {
bridge[cur][i] = bridge[i][cur] = true;
}
}
}
}
vis[cur] = 2;
}