This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ei1333/library
#include "graph/tree/rmq-lowest-common-ancestor.hpp"
オイラーツアーとスパーステーブルによって最小共通祖先を求める.
辺属性のオイラーツアーをする. すべての頂点について, その頂点 $k$ に最初に到達した時刻 $in[k]$ と深さ $dep[k]$ を求めておく. 頂点 $u, v$ の最小共通祖先は区間 $[in[u], in[v]]$ の要素のうち深さが最小となる頂点である. 区間の最小値なのでスパーステーブルにより前計算しておくと, クエリあたり $O(1)$ で処理できる.
build()
lca(u, v)
u
v
lca()
#pragma once #include "../graph-template.hpp" #include "../../structure/others/sparse-table.hpp" /** * @brief RMQ-Lowest-Common-Ancestor(最小共通祖先) * @docs docs/rmq-lowest-common-ancestor.md **/ template< typename T = int > struct RMQLowestCommonAncestor : Graph< T > { public: using Graph< T >::Graph; using Graph< T >::g; using F = function< int(int, int) >; void build(int root = 0) { ord.reserve(g.size() * 2 - 1); dep.reserve(g.size() * 2 - 1); in.resize(g.size()); dfs(root, -1, 0); vector< int > vs(g.size() * 2 - 1); iota(begin(vs), end(vs), 0); F f = [&](int a, int b) { return dep[a] < dep[b] ? a : b; }; st = get_sparse_table(vs, f); } int lca(int x, int y) const { if(in[x] > in[y]) swap(x, y); return x == y ? x : ord[st.fold(in[x], in[y])]; } private: vector< int > ord, dep, in; SparseTable< int, F > st; void dfs(int idx, int par, int d) { in[idx] = (int) ord.size(); ord.emplace_back(idx); dep.emplace_back(d); for(auto &to : g[idx]) { if(to != par) { dfs(to, idx, d + 1); ord.emplace_back(idx); dep.emplace_back(d); } } } };
#line 2 "graph/tree/rmq-lowest-common-ancestor.hpp" #line 2 "graph/graph-template.hpp" /** * @brief Graph Template(グラフテンプレート) */ 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 { vector< vector< Edge< T > > > g; int es; Graph() = default; explicit Graph(int n) : g(n), es(0) {} 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; cin >> a >> b; a += padding; b += padding; T c = T(1); if(weighted) cin >> c; if(directed) add_directed_edge(a, b, c); else add_edge(a, b, c); } } inline vector< Edge< T > > &operator[](const int &k) { return g[k]; } inline const vector< Edge< T > > &operator[](const int &k) const { return g[k]; } }; template< typename T = int > using Edges = vector< Edge< T > >; #line 1 "structure/others/sparse-table.hpp" /** * @brief Sparse-Table(スパーステーブル) * @docs docs/sparse-table.md */ template< typename T, typename F > struct SparseTable { F f; vector< vector< T > > st; vector< int > lookup; SparseTable() = default; explicit SparseTable(const vector< T > &v, const F &f) : f(f) { const int n = (int) v.size(); const int b = 32 - __builtin_clz(n); st.assign(b, vector< T >(n)); for(int i = 0; i < v.size(); i++) { st[0][i] = v[i]; } for(int i = 1; i < b; i++) { for(int j = 0; j + (1 << i) <= n; j++) { st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]); } } lookup.resize(v.size() + 1); for(int i = 2; i < lookup.size(); i++) { lookup[i] = lookup[i >> 1] + 1; } } inline T fold(int l, int r) const { int b = lookup[r - l]; return f(st[b][l], st[b][r - (1 << b)]); } }; template< typename T, typename F > SparseTable< T, F > get_sparse_table(const vector< T > &v, const F &f) { return SparseTable< T, F >(v, f); } #line 5 "graph/tree/rmq-lowest-common-ancestor.hpp" /** * @brief RMQ-Lowest-Common-Ancestor(最小共通祖先) * @docs docs/rmq-lowest-common-ancestor.md **/ template< typename T = int > struct RMQLowestCommonAncestor : Graph< T > { public: using Graph< T >::Graph; using Graph< T >::g; using F = function< int(int, int) >; void build(int root = 0) { ord.reserve(g.size() * 2 - 1); dep.reserve(g.size() * 2 - 1); in.resize(g.size()); dfs(root, -1, 0); vector< int > vs(g.size() * 2 - 1); iota(begin(vs), end(vs), 0); F f = [&](int a, int b) { return dep[a] < dep[b] ? a : b; }; st = get_sparse_table(vs, f); } int lca(int x, int y) const { if(in[x] > in[y]) swap(x, y); return x == y ? x : ord[st.fold(in[x], in[y])]; } private: vector< int > ord, dep, in; SparseTable< int, F > st; void dfs(int idx, int par, int d) { in[idx] = (int) ord.size(); ord.emplace_back(idx); dep.emplace_back(d); for(auto &to : g[idx]) { if(to != par) { dfs(to, idx, d + 1); ord.emplace_back(idx); dep.emplace_back(d); } } } };