sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

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

:heavy_check_mark: test/yosupo/biconnected_components.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/biconnected_components"

#include "../../graph/biconnected_components.hpp"

#include <bits/stdc++.h>

#include "../../graph/lowlink.hpp"
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    for (int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    Lowlink low(G);
    auto blocks = biconnected_components(G, low);
    cout << blocks.size() << endl;
    for (auto& block : blocks) {
        cout << block.size();
        for (int i : block) cout << " " << i;
        cout << "\n";
    }
}
#line 1 "test/yosupo/biconnected_components.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/biconnected_components"

#line 2 "graph/biconnected_components.hpp"
#include <set>
#include <stack>
#include <unordered_map>
#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) {
        low[v] = ord[v] = k++;
        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 9 "graph/biconnected_components.hpp"

std::vector<std::vector<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<int>> blocks;

    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
                    std::set<int> block;
                    while (true) {
                        auto e = st.top();
                        st.pop();
                        block.insert(e.first);
                        block.insert(e.second);
                        if (e.first == v) {
                            break;
                        }
                    }
                    blocks.emplace_back(block.begin(), block.end());
                }
            }
        }
    };

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

    return blocks;
}

// B: number of blocks, C: number of cut vertices
// 0 through B - 1: block
// B through B + C - 1: cut
std::vector<std::vector<int>> block_cut_tree(
    const std::vector<std::vector<int>>& blocks, const std::vector<int>& cuts) {
    const int B = blocks.size();
    std::vector<std::vector<int>> bct(B + (int)cuts.size());
    std::unordered_map<int, int> cut_idx;
    for (int i = 0; i < (int)cuts.size(); ++i) cut_idx[cuts[i]] = i;

    for (int i = 0; i < (int)blocks.size(); ++i) {
        auto& block = blocks[i];
        for (int v : block) {
            if (cut_idx.contains(v)) {
                int j = B + cut_idx[v];
                bct[i].push_back(j);
                bct[j].push_back(i);
            }
        }
    }

    return bct;
}
#line 4 "test/yosupo/biconnected_components.test.cpp"

#include <bits/stdc++.h>

#line 8 "test/yosupo/biconnected_components.test.cpp"
using namespace std;
using ll = long long;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    for (int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    Lowlink low(G);
    auto blocks = biconnected_components(G, low);
    cout << blocks.size() << endl;
    for (auto& block : blocks) {
        cout << block.size();
        for (int i : block) cout << " " << i;
        cout << "\n";
    }
}
Back to top page