sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

View the Project on GitHub sotanishy/cp-library-cpp

:heavy_check_mark: Biconnected Components
(graph/biconnected_components.hpp)

Description

二重頂点連結成分は,どの1頂点を取り除いても連結であるような部分グラフである.つまり,関節点を含まない部分グラフである.

関節点と二重頂点連結成分を結んで構成した木は block cut tree と呼ばれる.

空間計算量: $O(V + E)$

Operations

Note

多重辺は予め取り除いておく必要がある (多重辺を取り除いても二重頂点連結成分は変化しない).

また,孤立点にも注意する (孤立点はそれで一つの二重頂点連結成分となるが,この関数は辺を返すため,孤立点からなる成分は含まれない).

Depends on

Verified with

Code

#pragma once
#include <stack>
#include <utility>
#include <vector>

#include "lowlink.hpp"

std::vector<std::vector<std::pair<int, int>>> biconnected_components(
    const std::vector<std::vector<int>>& G, const Lowlink& low) {
    std::vector<bool> used(G.size());
    std::stack<std::pair<int, int>> st;
    std::vector<std::vector<std::pair<int, int>>> bc;

    auto dfs = [&](auto& dfs, int v, int p) -> void {
        used[v] = true;
        for (int c : G[v]) {
            if (c == p) continue;
            if (!used[c] || low.ord[c] < low.ord[v]) {
                st.emplace(v, c);
            }
            if (!used[c]) {
                dfs(dfs, c, v);
                if (low.ord[v] <= low.low[c]) {  // v is an articulation point
                    bc.emplace_back();
                    while (true) {
                        auto e = st.top();
                        st.pop();
                        bc.back().push_back(e);
                        if (e.first == v) {
                            break;
                        }
                    }
                }
            }
        }
    };

    for (int v = 0; v < (int)G.size(); ++v) {
        if (!used[v]) dfs(dfs, v, -1);
    }

    return bc;
}
#line 2 "graph/biconnected_components.hpp"
#include <stack>
#include <utility>
#include <vector>

#line 2 "graph/lowlink.hpp"
#include <algorithm>
#line 5 "graph/lowlink.hpp"

class Lowlink {
   public:
    std::vector<int> ord, low;
    std::vector<std::pair<int, int>> bridge;
    std::vector<int> articulation;

    Lowlink() = default;
    explicit Lowlink(const std::vector<std::vector<int>>& G)
        : ord(G.size(), -1), low(G.size()), G(G) {
        for (int i = 0; i < (int)G.size(); ++i) {
            if (ord[i] == -1) dfs(i, -1);
        }
    }

    bool is_bridge(int u, int v) const {
        if (ord[u] > ord[v]) std::swap(u, v);
        return ord[u] < low[v];
    }

   private:
    std::vector<std::vector<int>> G;
    int k = 0;

    void dfs(int v, int p) {
        ord[v] = k++;
        low[v] = ord[v];
        bool is_articulation = false, checked = false;
        int cnt = 0;
        for (int c : G[v]) {
            if (c == p && !checked) {
                checked = true;
                continue;
            }
            if (ord[c] == -1) {
                ++cnt;
                dfs(c, v);
                low[v] = std::min(low[v], low[c]);
                if (p != -1 && ord[v] <= low[c]) is_articulation = true;
                if (ord[v] < low[c]) bridge.push_back(std::minmax(v, c));
            } else {
                low[v] = std::min(low[v], ord[c]);
            }
        }
        if (p == -1 && cnt > 1) is_articulation = true;
        if (is_articulation) articulation.push_back(v);
    }
};
#line 7 "graph/biconnected_components.hpp"

std::vector<std::vector<std::pair<int, int>>> biconnected_components(
    const std::vector<std::vector<int>>& G, const Lowlink& low) {
    std::vector<bool> used(G.size());
    std::stack<std::pair<int, int>> st;
    std::vector<std::vector<std::pair<int, int>>> bc;

    auto dfs = [&](auto& dfs, int v, int p) -> void {
        used[v] = true;
        for (int c : G[v]) {
            if (c == p) continue;
            if (!used[c] || low.ord[c] < low.ord[v]) {
                st.emplace(v, c);
            }
            if (!used[c]) {
                dfs(dfs, c, v);
                if (low.ord[v] <= low.low[c]) {  // v is an articulation point
                    bc.emplace_back();
                    while (true) {
                        auto e = st.top();
                        st.pop();
                        bc.back().push_back(e);
                        if (e.first == v) {
                            break;
                        }
                    }
                }
            }
        }
    };

    for (int v = 0; v < (int)G.size(); ++v) {
        if (!used[v]) dfs(dfs, v, -1);
    }

    return bc;
}
Back to top page