Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: Strongly Connected Components (強連結成分分解) (graph/connected-components/strongly-connected-components.hpp)

与えられた有向グラフを強連結成分分解する。

グラフの任意の $2$ 頂点間に有向路が存在するとき、有向グラフが強連結であるとよぶ。強連結成分は、極大で強連結な部分グラフである。

各頂点の DFS における訪問順 ord と、DFS 木の部分木から到達できる訪問中の頂点の最小訪問順 low を求める。ordlow が等しい頂点を根とする部分が $1$ つの強連結成分となる。

強連結成分を縮約後の頂点とそれらを結ぶ辺からなるグラフは DAG になっている。

コンストラクタ

explicit StronglyConnectedComponents(int n)

頂点数 n のグラフで初期化します。

計算量

  • $O(n)$

build

void build()

強連結成分分解する。dag には縮約後の頂点と辺からなる DAG が格納される。comp には各頂点が属する強連結成分の頂点番号がトポロジカル順で格納される。group には各強連結成分について、それに属する頂点が格納される。辺を追加した後に再度呼び出すこともできる。

計算量

  • $O(E + V)$

operator[]

int operator[](int k) const

頂点 $k$ が属する強連結成分の番号を返す。

計算量

  • $O(1)$

Depends on

Required by

Verified with

Code

#pragma once

#include <algorithm>
#include <cstddef>
#include <utility>
#include <vector>

#include "../graph-template.hpp"

template <typename T = int>
struct StronglyConnectedComponents : Graph<T> {
  using Graph<T>::Graph;
  using Graph<T>::g;
  std::vector<int> comp;
  Graph<T> dag;
  std::vector<std::vector<int>> group;

  void build() {
    comp.assign(g.size(), -1);
    order.assign(g.size(), -1);
    low.resize(g.size());
    in_stack.assign(g.size(), false);
    dfs_stack.clear();
    dfs_stack.reserve(g.size());
    vertex_stack.clear();
    vertex_stack.reserve(g.size());
    int now = 0, ptr = 0;
    for (std::size_t i = 0; i < g.size(); i++) {
      if (order[i] == -1) dfs(static_cast<int>(i), now, ptr);
    }
    for (int& component : comp) component = ptr - 1 - component;

    dag = Graph<T>(ptr);
    for (std::size_t i = 0; i < g.size(); i++) {
      for (auto& e : g[i]) {
        int x = comp[e.from], y = comp[e.to];
        if (x == y) continue;
        dag.add_directed_edge(x, y, e.cost);
      }
    }
    group.assign(ptr, {});
    for (std::size_t i = 0; i < g.size(); i++) {
      group[comp[i]].emplace_back(i);
    }
  }

  int operator[](int k) const { return comp[k]; }

 private:
  std::vector<int> order, low, in_stack, vertex_stack;
  std::vector<std::pair<int, std::size_t>> dfs_stack;

  void dfs(int root, int& now, int& component_count) {
    dfs_stack.clear();
    order[root] = low[root] = now++;
    in_stack[root] = true;
    vertex_stack.push_back(root);
    dfs_stack.emplace_back(root, 0);
    while (!dfs_stack.empty()) {
      auto& [v, edge_index] = dfs_stack.back();
      if (edge_index < g[v].size()) {
        int to = g[v][edge_index++].to;
        if (order[to] == -1) {
          order[to] = low[to] = now++;
          in_stack[to] = true;
          vertex_stack.push_back(to);
          dfs_stack.emplace_back(to, 0);
        } else if (in_stack[to]) {
          low[v] = std::min(low[v], order[to]);
        }
        continue;
      }

      dfs_stack.pop_back();
      if (!dfs_stack.empty()) {
        int parent = dfs_stack.back().first;
        low[parent] = std::min(low[parent], low[v]);
      }
      if (low[v] != order[v]) continue;
      while (true) {
        int u = vertex_stack.back();
        vertex_stack.pop_back();
        in_stack[u] = false;
        comp[u] = component_count;
        if (u == v) break;
      }
      component_count++;
    }
  }
};
#line 2 "graph/connected-components/strongly-connected-components.hpp"

#include <algorithm>
#include <cstddef>
#include <utility>
#include <vector>

#line 2 "graph/graph-template.hpp"

#line 4 "graph/graph-template.hpp"
#include <iostream>
#line 6 "graph/graph-template.hpp"

template <typename T = int>
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1)
      : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template <typename T = int>
struct Graph {
  std::vector<std::vector<Edge<T> > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  std::size_t size() const { return g.size(); }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(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 a, b;
      std::cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if (weighted) std::cin >> c;
      if (directed)
        add_directed_edge(a, b, c);
      else
        add_edge(a, b, c);
    }
  }

  inline std::vector<Edge<T> >& operator[](const int& k) { return g[k]; }

  inline const std::vector<Edge<T> >& operator[](const int& k) const {
    return g[k];
  }
};

template <typename T = int>
using Edges = std::vector<Edge<T> >;
#line 9 "graph/connected-components/strongly-connected-components.hpp"

template <typename T = int>
struct StronglyConnectedComponents : Graph<T> {
  using Graph<T>::Graph;
  using Graph<T>::g;
  std::vector<int> comp;
  Graph<T> dag;
  std::vector<std::vector<int>> group;

  void build() {
    comp.assign(g.size(), -1);
    order.assign(g.size(), -1);
    low.resize(g.size());
    in_stack.assign(g.size(), false);
    dfs_stack.clear();
    dfs_stack.reserve(g.size());
    vertex_stack.clear();
    vertex_stack.reserve(g.size());
    int now = 0, ptr = 0;
    for (std::size_t i = 0; i < g.size(); i++) {
      if (order[i] == -1) dfs(static_cast<int>(i), now, ptr);
    }
    for (int& component : comp) component = ptr - 1 - component;

    dag = Graph<T>(ptr);
    for (std::size_t i = 0; i < g.size(); i++) {
      for (auto& e : g[i]) {
        int x = comp[e.from], y = comp[e.to];
        if (x == y) continue;
        dag.add_directed_edge(x, y, e.cost);
      }
    }
    group.assign(ptr, {});
    for (std::size_t i = 0; i < g.size(); i++) {
      group[comp[i]].emplace_back(i);
    }
  }

  int operator[](int k) const { return comp[k]; }

 private:
  std::vector<int> order, low, in_stack, vertex_stack;
  std::vector<std::pair<int, std::size_t>> dfs_stack;

  void dfs(int root, int& now, int& component_count) {
    dfs_stack.clear();
    order[root] = low[root] = now++;
    in_stack[root] = true;
    vertex_stack.push_back(root);
    dfs_stack.emplace_back(root, 0);
    while (!dfs_stack.empty()) {
      auto& [v, edge_index] = dfs_stack.back();
      if (edge_index < g[v].size()) {
        int to = g[v][edge_index++].to;
        if (order[to] == -1) {
          order[to] = low[to] = now++;
          in_stack[to] = true;
          vertex_stack.push_back(to);
          dfs_stack.emplace_back(to, 0);
        } else if (in_stack[to]) {
          low[v] = std::min(low[v], order[to]);
        }
        continue;
      }

      dfs_stack.pop_back();
      if (!dfs_stack.empty()) {
        int parent = dfs_stack.back().first;
        low[parent] = std::min(low[parent], low[v]);
      }
      if (low[v] != order[v]) continue;
      while (true) {
        int u = vertex_stack.back();
        vertex_stack.pop_back();
        in_stack[u] = false;
        comp[u] = component_count;
        if (u == v) break;
      }
      component_count++;
    }
  }
};
Back to top page