serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: 強連結成分分解(Tarjan)
(library/graph/connected_components/strongly_connected_components.hpp)

強連結成分分解(Tarjan)

できること

計算量

$O(V+E)$

使い方

auto [groups, ids] = scc(G);

Depends on

Verified with

Code

#pragma once
#include "library/graph/base/graph.hpp"
pair<vector<vector<int>>, vector<int>> scc(const Graph &G) {
    int N = G.size(), cnt = 0, now = 0;
    vector<int> ids(N), low(N), ord(N, -1), pth;
    auto dfs = [&](auto &f, int v) -> void {
        low[v] = ord[v] = now++;
        pth.emplace_back(v);
        // lowlink
        for (auto &&[from, to, cost, idx] : G[v]) {
            if (ord[to] == -1) {
                f(f, to);
                low[v] = min(low[v], low[to]);
            } else {
                low[v] = min(low[v], ord[to]);
            }
        }
        if (low[v] == ord[v]) {
            while (1) {
                int u = pth.back();
                pth.pop_back();
                ord[u] = N, ids[u] = cnt;
                if (u == v) break;
            }
            ++cnt;
        }
    };
    for (int v = 0; v < N; ++v) {
        if (ord[v] == -1) dfs(dfs, v);
    }
    for (int v = 0; v < N; ++v) {
        ids[v] = cnt - 1 - ids[v];
    }
    vector<int> c(cnt);
    vector<vector<int>> groups(cnt);
    for (auto &&v : ids) ++c[v];
    for (int i = 0; i < cnt; ++i) groups[i].reserve(c[i]);
    for (int i = 0; i < N; ++i) groups[ids[i]].push_back(i);
    return {groups, ids};
}
#line 2 "library/graph/base/edge.hpp"
struct Edge {
    int from, to;
    long long cost;
    int idx;
    Edge(int from, int to, long long cost = 1, int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
};
#line 3 "library/graph/base/graph.hpp"
struct Graph {
    int N;
    vector<vector<Edge>> G;
    int es;
    Graph() = default;
    Graph(int N) : N(N), G(N), es(0) {}
    const vector<Edge> &operator[](int v) const { return G[v]; }
    int size() const { return N; }
    void add(int from, int to, long long cost = 1) {
        G[from].push_back(Edge(from, to, cost, es++));
    }
    void add_both(int from, int to, long long cost = 1) {
        G[from].push_back(Edge(from, to, cost, es));
        G[to].push_back(Edge(to, from, cost, es++));
    }
    void read(int M, int padding = -1, bool weighted = false,
              bool directed = false) {
        for (int i = 0; i < M; i++) {
            int u, v;
            cin >> u >> v;
            u += padding, v += padding;
            long long cost = 1ll;
            if (weighted) cin >> cost;
            if (directed) {
                add(u, v, cost);
            } else {
                add_both(u, v, cost);
            }
        }
    }
};
#line 3 "library/graph/connected_components/strongly_connected_components.hpp"
pair<vector<vector<int>>, vector<int>> scc(const Graph &G) {
    int N = G.size(), cnt = 0, now = 0;
    vector<int> ids(N), low(N), ord(N, -1), pth;
    auto dfs = [&](auto &f, int v) -> void {
        low[v] = ord[v] = now++;
        pth.emplace_back(v);
        // lowlink
        for (auto &&[from, to, cost, idx] : G[v]) {
            if (ord[to] == -1) {
                f(f, to);
                low[v] = min(low[v], low[to]);
            } else {
                low[v] = min(low[v], ord[to]);
            }
        }
        if (low[v] == ord[v]) {
            while (1) {
                int u = pth.back();
                pth.pop_back();
                ord[u] = N, ids[u] = cnt;
                if (u == v) break;
            }
            ++cnt;
        }
    };
    for (int v = 0; v < N; ++v) {
        if (ord[v] == -1) dfs(dfs, v);
    }
    for (int v = 0; v < N; ++v) {
        ids[v] = cnt - 1 - ids[v];
    }
    vector<int> c(cnt);
    vector<vector<int>> groups(cnt);
    for (auto &&v : ids) ++c[v];
    for (int i = 0; i < cnt; ++i) groups[i].reserve(c[i]);
    for (int i = 0; i < N; ++i) groups[ids[i]].push_back(i);
    return {groups, ids};
}
Back to top page