Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: test/unittest/strongly-connected-components.test.cpp

Depends on

Code

// competitive-verifier: STANDALONE

#include "../../graph/connected-components/strongly-connected-components.hpp"

#include <cassert>
#include <random>
#include <vector>

int main() {
  {
    StronglyConnectedComponents<> graph(8);
    graph.add_directed_edge(0, 1);
    graph.add_directed_edge(1, 2);
    graph.add_directed_edge(2, 0);
    graph.add_directed_edge(2, 3);
    graph.add_directed_edge(3, 4);
    graph.add_directed_edge(4, 3);
    graph.add_directed_edge(4, 5);
    graph.add_directed_edge(6, 7);
    graph.build();

    assert(graph[0] == graph[1] && graph[1] == graph[2]);
    assert(graph[3] == graph[4]);
    assert(graph[2] != graph[3]);
    assert(graph[3] != graph[5]);
    assert(graph[6] != graph[7]);
    assert(graph[2] < graph[3] && graph[3] < graph[5]);
    assert(graph[6] < graph[7]);

    graph.add_directed_edge(5, 0);
    graph.build();
    for (int v = 1; v <= 5; v++) assert(graph[v] == graph[0]);
    int grouped_vertices = 0;
    for (const auto& component : graph.group) {
      grouped_vertices += static_cast<int>(component.size());
    }
    assert(grouped_vertices == 8);
  }

  std::mt19937 rng(123456789);
  for (int n = 1; n <= 30; n++) {
    for (int iteration = 0; iteration < 30; iteration++) {
      StronglyConnectedComponents<> graph(n);
      std::vector<std::vector<bool>> reachable(n, std::vector<bool>(n, false));
      for (int v = 0; v < n; v++) reachable[v][v] = true;
      for (int from = 0; from < n; from++) {
        for (int to = 0; to < n; to++) {
          if (rng() % 5 == 0) {
            graph.add_directed_edge(from, to);
            reachable[from][to] = true;
          }
        }
      }
      for (int k = 0; k < n; k++) {
        for (int from = 0; from < n; from++) {
          for (int to = 0; to < n; to++) {
            reachable[from][to] =
                reachable[from][to] || (reachable[from][k] && reachable[k][to]);
          }
        }
      }

      graph.build();
      for (int from = 0; from < n; from++) {
        for (int to = 0; to < n; to++) {
          bool same_component = reachable[from][to] && reachable[to][from];
          assert((graph[from] == graph[to]) == same_component);
          if (reachable[from][to] && graph[from] != graph[to]) {
            assert(graph[from] < graph[to]);
          }
        }
      }
    }
  }

  {
    constexpr int kVertices = 300000;
    StronglyConnectedComponents<> graph(kVertices);
    for (int v = 1; v < kVertices; v++) {
      graph.add_directed_edge(v - 1, v);
    }
    graph.build();
    assert(graph.group.size() == kVertices);
    assert(graph[0] == 0);
    assert(graph[kVertices - 1] == kVertices - 1);
  }
}
#line 1 "test/unittest/strongly-connected-components.test.cpp"
// competitive-verifier: STANDALONE

#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++;
    }
  }
};
#line 4 "test/unittest/strongly-connected-components.test.cpp"

#include <cassert>
#include <random>
#line 8 "test/unittest/strongly-connected-components.test.cpp"

int main() {
  {
    StronglyConnectedComponents<> graph(8);
    graph.add_directed_edge(0, 1);
    graph.add_directed_edge(1, 2);
    graph.add_directed_edge(2, 0);
    graph.add_directed_edge(2, 3);
    graph.add_directed_edge(3, 4);
    graph.add_directed_edge(4, 3);
    graph.add_directed_edge(4, 5);
    graph.add_directed_edge(6, 7);
    graph.build();

    assert(graph[0] == graph[1] && graph[1] == graph[2]);
    assert(graph[3] == graph[4]);
    assert(graph[2] != graph[3]);
    assert(graph[3] != graph[5]);
    assert(graph[6] != graph[7]);
    assert(graph[2] < graph[3] && graph[3] < graph[5]);
    assert(graph[6] < graph[7]);

    graph.add_directed_edge(5, 0);
    graph.build();
    for (int v = 1; v <= 5; v++) assert(graph[v] == graph[0]);
    int grouped_vertices = 0;
    for (const auto& component : graph.group) {
      grouped_vertices += static_cast<int>(component.size());
    }
    assert(grouped_vertices == 8);
  }

  std::mt19937 rng(123456789);
  for (int n = 1; n <= 30; n++) {
    for (int iteration = 0; iteration < 30; iteration++) {
      StronglyConnectedComponents<> graph(n);
      std::vector<std::vector<bool>> reachable(n, std::vector<bool>(n, false));
      for (int v = 0; v < n; v++) reachable[v][v] = true;
      for (int from = 0; from < n; from++) {
        for (int to = 0; to < n; to++) {
          if (rng() % 5 == 0) {
            graph.add_directed_edge(from, to);
            reachable[from][to] = true;
          }
        }
      }
      for (int k = 0; k < n; k++) {
        for (int from = 0; from < n; from++) {
          for (int to = 0; to < n; to++) {
            reachable[from][to] =
                reachable[from][to] || (reachable[from][k] && reachable[k][to]);
          }
        }
      }

      graph.build();
      for (int from = 0; from < n; from++) {
        for (int to = 0; to < n; to++) {
          bool same_component = reachable[from][to] && reachable[to][from];
          assert((graph[from] == graph[to]) == same_component);
          if (reachable[from][to] && graph[from] != graph[to]) {
            assert(graph[from] < graph[to]);
          }
        }
      }
    }
  }

  {
    constexpr int kVertices = 300000;
    StronglyConnectedComponents<> graph(kVertices);
    for (int v = 1; v < kVertices; v++) {
      graph.add_directed_edge(v - 1, v);
    }
    graph.build();
    assert(graph.group.size() == kVertices);
    assert(graph[0] == 0);
    assert(graph[kVertices - 1] == kVertices - 1);
  }
}
Back to top page