This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ruthen71/rcpl
#include "graph/strongly_connected_component.hpp"
strongly_connected_component(G)
#pragma once #include "graph/graph_template.hpp" template <class T> std::vector<std::vector<int>> strongly_connected_component(Graph<T>& g) { int n = int(g.size()); int now_ord = 0, group_num = 0; std::vector<int> visited, low(n), ord(n, -1), ids(n); visited.reserve(n); auto dfs = [&](auto f, int cur) -> void { low[cur] = ord[cur] = now_ord++; visited.push_back(cur); for (auto& e : g[cur]) { if (ord[e.to] == -1) { f(f, e.to); low[cur] = std::min(low[cur], low[e.to]); } else { low[cur] = std::min(low[cur], ord[e.to]); } } if (low[cur] == ord[cur]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = n; ids[u] = group_num; if (u == cur) break; } group_num++; } }; for (int i = 0; i < n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto& x : ids) { x = group_num - 1 - x; } std::vector<int> counts(group_num); for (auto& x : ids) counts[x]++; std::vector<std::vector<int>> groups(group_num); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < n; i++) { groups[ids[i]].push_back(i); } return groups; }
#line 2 "graph/strongly_connected_component.hpp" #line 2 "graph/graph_template.hpp" #include <vector> template <class T> struct Edge { int from, to; T cost; int id; Edge() = default; Edge(int from, int to, T cost = 1, int id = -1) : from(from), to(to), cost(cost), id(id) {} friend std::ostream &operator<<(std::ostream &os, const Edge<T> &e) { // output format: "{ id : from -> to, cost }" return os << "{ " << e.id << " : " << e.from << " -> " << e.to << ", " << e.cost << " }"; } }; template <class T> using Edges = std::vector<Edge<T>>; template <class T> using Graph = std::vector<std::vector<Edge<T>>>; #line 4 "graph/strongly_connected_component.hpp" template <class T> std::vector<std::vector<int>> strongly_connected_component(Graph<T>& g) { int n = int(g.size()); int now_ord = 0, group_num = 0; std::vector<int> visited, low(n), ord(n, -1), ids(n); visited.reserve(n); auto dfs = [&](auto f, int cur) -> void { low[cur] = ord[cur] = now_ord++; visited.push_back(cur); for (auto& e : g[cur]) { if (ord[e.to] == -1) { f(f, e.to); low[cur] = std::min(low[cur], low[e.to]); } else { low[cur] = std::min(low[cur], ord[e.to]); } } if (low[cur] == ord[cur]) { while (true) { int u = visited.back(); visited.pop_back(); ord[u] = n; ids[u] = group_num; if (u == cur) break; } group_num++; } }; for (int i = 0; i < n; i++) { if (ord[i] == -1) dfs(dfs, i); } for (auto& x : ids) { x = group_num - 1 - x; } std::vector<int> counts(group_num); for (auto& x : ids) counts[x]++; std::vector<std::vector<int>> groups(group_num); for (int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for (int i = 0; i < n; i++) { groups[ids[i]].push_back(i); } return groups; }