This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ruthen71/rcpl
#include "graph/low_link.hpp"
LowLink llink(G);
llink.articulations
llink.bridges
#pragma once #include "graph/graph_template.hpp" template <class T> struct LowLink { int n; std::vector<int> ord, low; std::vector<int> articulations; std::vector<int> roots; std::vector<std::pair<int, int>> bridges; // edges {u, v} (u < v) std::vector<std::vector<int>> dfs_tree; LowLink(const Graph<T>& g) : n(int(g.size())) { ord.assign(n, -1); low.assign(n, -1); dfs_tree.resize(n); int ord_id = 0; auto dfs = [&](auto f, int cur, int par) -> void { low[cur] = ord[cur] = ord_id++; bool is_articulation = false; for (auto& e : g[cur]) { if (ord[e.to] == -1) { // DFS 木上の辺に対する処理 f(f, e.to, cur); dfs_tree[cur].push_back(e.to); // e が DFS 木に含まれているので後退辺をすでに通った low[e.to] を使って更新して良い low[cur] = std::min(low[cur], low[e.to]); is_articulation |= (par != -1) and (ord[cur] <= low[e.to]); if (ord[cur] < low[e.to]) { bridges.emplace_back(std::minmax(cur, e.to)); } } else if (e.to != par) { // 後退辺に対する処理 // Todo: multiple edges low[cur] = std::min(low[cur], ord[e.to]); } } is_articulation |= par == -1 and int(dfs_tree[cur].size()) > 1; if (is_articulation) articulations.push_back(cur); return; }; for (int i = 0; i < n; i++) { if (ord[i] == -1) { roots.push_back(i); dfs(dfs, i, -1); } } } // 連結成分数 int count_components() { return int(roots.size()); } // 頂点 x を取り除くともともと 1 つだった連結成分がいくつになるか int count_components_remove(int x) { if (std::binary_search(roots.begin(), roots.end(), x)) { int c = int(dfs_tree[x].size()); return c; } else { int c = 0; for (auto& e : dfs_tree[x]) { if (ord[x] <= low[e]) c++; } // 親の分で +1 return c + 1; } } };
#line 2 "graph/low_link.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/low_link.hpp" template <class T> struct LowLink { int n; std::vector<int> ord, low; std::vector<int> articulations; std::vector<int> roots; std::vector<std::pair<int, int>> bridges; // edges {u, v} (u < v) std::vector<std::vector<int>> dfs_tree; LowLink(const Graph<T>& g) : n(int(g.size())) { ord.assign(n, -1); low.assign(n, -1); dfs_tree.resize(n); int ord_id = 0; auto dfs = [&](auto f, int cur, int par) -> void { low[cur] = ord[cur] = ord_id++; bool is_articulation = false; for (auto& e : g[cur]) { if (ord[e.to] == -1) { // DFS 木上の辺に対する処理 f(f, e.to, cur); dfs_tree[cur].push_back(e.to); // e が DFS 木に含まれているので後退辺をすでに通った low[e.to] を使って更新して良い low[cur] = std::min(low[cur], low[e.to]); is_articulation |= (par != -1) and (ord[cur] <= low[e.to]); if (ord[cur] < low[e.to]) { bridges.emplace_back(std::minmax(cur, e.to)); } } else if (e.to != par) { // 後退辺に対する処理 // Todo: multiple edges low[cur] = std::min(low[cur], ord[e.to]); } } is_articulation |= par == -1 and int(dfs_tree[cur].size()) > 1; if (is_articulation) articulations.push_back(cur); return; }; for (int i = 0; i < n; i++) { if (ord[i] == -1) { roots.push_back(i); dfs(dfs, i, -1); } } } // 連結成分数 int count_components() { return int(roots.size()); } // 頂点 x を取り除くともともと 1 つだった連結成分がいくつになるか int count_components_remove(int x) { if (std::binary_search(roots.begin(), roots.end(), x)) { int c = int(dfs_tree[x].size()); return c; } else { int c = 0; for (auto& e : dfs_tree[x]) { if (ord[x] <= low[e]) c++; } // 親の分で +1 return c + 1; } } };