【笔记】图的连通性

1 Template

1.1 割点与桥

void dfs (int u, int fa) {
	low[u] = dfn[u] = ++_dfn; int siz = 0;
	for (int v : G[u]) if (v^fa) {
		if (!dfn[v]) {
			dfs(v, u), siz++;
			low[u] = min(low[u], low[v]);
			if (low[v] > dfn[u]) cnte++;  // 桥
			tag[u] |= fa ? low[v] >= dfn[u] : siz >= 2;  // 割点
		} else {
			low[u] = min(low[u], dfn[v]);
		}
	}
}

1.2 缩点(用于有向图

void dfs (int u, int fa) {
	st.push(u), low[u] = dfn[u] = ++_dfn;
	for (int v : G[u]) if (v^fa) {
		if (!dfn[v]) {
			dfs(v, u);
			low[u] = min(low[u], low[v]);
		} else
			low[u] = min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u]) {
		++_dcc; static int t;
		do dcc[t = st.top()] = _dcc, st.pop();
		while (t != u);
	}
}
posted @ 2024-07-09 21:27  CloudWings  阅读(24)  评论(0)    收藏  举报