ContinueLib

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Continue765/ContinueLib

:heavy_check_mark: EBCC
(graph/components/ebcc.hpp)

EBCC

定义于头文件 ebcc.hpp

template <typename T>
pair<int, vector<int>> EBCC(const Undigraph<T>& g);

求无向图 g 的边双连通分量,返回 {cnt, comp}。同一分量内任意两点之间存在两条边不相交的路径。

参数

参数 说明
g 无向图

返回值

返回 {cnt, comp}:cnt 为边双连通分量个数,comp[u] 为点 u 所属分量编号,$0 \le \text{comp}[u] < \text{cnt}$。

复杂度

$O(n + m)$,$n$ 为点数,$m$ 为边数。

示例

Undigraph<int> g(3);
g.add(0, 1);
g.add(1, 2);

auto [cnt, comp] = EBCC(g);
for (int u = 0; u < g.n; u++) {
    int id = comp[u];
}

Depends on

Verified with

Code

#pragma once
#include <graph/graph.hpp>

template <typename T>
pair<int, vector<int>> EBCC(const Undigraph<T>& g) {
    int n = g.n;
    vector<int> stk;
    vector<int> dfn(n, -1), low(n), comp(n, -1);
    int cur = 0, cnt = 0;
    auto dfs = [&](auto&& self, int u, int parentEdge) -> void {
        dfn[u] = low[u] = cur++;
        stk.push_back(u);
        for (auto [v, id] : g[u]) {
            if (id == parentEdge) {
                continue;
            }
            if (dfn[v] == -1) {
                self(self, v, id);
                low[u] = min(low[u], low[v]);
            } else {
                low[u] = min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            int v;
            do {
                v = stk.back();
                stk.pop_back();
                comp[v] = cnt;
            } while (v != u);
            cnt++;
        }
    };
    for (int u = 0; u < n; u++) {
        if (dfn[u] == -1) {
            dfs(dfs, u, -1);
        }
    }
    return {cnt, comp};
}
Back to top page