C++ Library for Competitive Programming
#include "emthrm/graph/tree/lowest_common_ancestor_by_euler_tour_technique.hpp"
根付き木のある2頂点に対して最も深い共通祖先である。
時間計算量 | |
---|---|
ダブリング版 | $\langle O(\lvert V \rvert \log{\lvert V \rvert}), O(\log{\lvert V \rvert}) \rangle$ |
Euler tour technique 版 | $\langle O(\lvert V \rvert \log{\lvert V \rvert}), O(1) \rangle$ |
template <typename CostType>
struct LowestCommonAncestorByDoubling;
CostType
:辺のコストを表す型名前 | 説明 | 備考 |
---|---|---|
std::vector<int> depth |
depth[i] は頂点 $i$ の深さを表す。 |
|
std::vector<CostType> dist |
dist[i] は根と頂点 $i$ の間の距離を表す。 |
cost-free 版は depth に同じである。 |
名前 | 効果・戻り値 | 要件 |
---|---|---|
explicit LowestCommonAncestorByDoubling(const std::vector<std::vector<Edge<CostType>>>& graph); |
木 $\mathrm{graph}$ に対してオブジェクトを構築する。 | |
void build(const int root = 0); |
根を $\mathrm{root}$ として構築する。 | |
int query(int u, int v) const; |
頂点 $u, v$ の最小共通祖先 | |
CostType distance(const int u, const int v) const; |
頂点 $u, v$ 間の距離 | |
int level_ancestor(int v, const int d) const; |
level ancestor problem $\mathrm{LA}(v, d)$。ただし $d \leq \mathrm{depth}(v)$ でなければ $-1$ を返す。 | |
int jump(const int u, const int v, const int d) const; |
頂点 $u$ から頂点 $v$ まで距離 $d$ だけ進んだときの頂点。ただし $d > \mathrm{dist}(u, v)$ を満たすときは $-1$ を返す。 | cost-free 版 |
template <typename CostType>
struct LowestCommonAncestor : EulerTourTechnique<CostType>;
CostType
:辺のコストを表す型名前 | 効果・戻り値 |
---|---|
explicit LowestCommonAncestor(const std::vector<std::vector<Edge<CostType>>>& graph, const int root = 0); |
根を $\mathrm{root}$ とする木 $\mathrm{graph}$ に対してオブジェクトを構築する。 |
int query(int u, int v) const; |
頂点 $u, v$ の最小共通祖先 |
level ancestor problem
Euler tour technique 版
#ifndef EMTHRM_GRAPH_TREE_LOWEST_COMMON_ANCESTOR_BY_EULER_TOUR_TECHNIQUE_HPP_
#define EMTHRM_GRAPH_TREE_LOWEST_COMMON_ANCESTOR_BY_EULER_TOUR_TECHNIQUE_HPP_
#include <algorithm>
#include <utility>
#include <vector>
#include "emthrm/data_structure/sparse_table.hpp"
#include "emthrm/graph/edge.hpp"
#include "emthrm/graph/tree/euler_tour_technique.hpp"
namespace emthrm {
template <typename CostType>
struct LowestCommonAncestor : EulerTourTechnique<CostType> {
explicit LowestCommonAncestor(
const std::vector<std::vector<Edge<CostType>>>& graph,
const int root = 0)
: EulerTourTechnique<CostType>(graph, root) {
const int n = this->preorder.size();
std::vector<std::pair<int, int>> nodes(n);
for (int i = 0; i < n; ++i) {
nodes[i] = {this->depth[i], this->preorder[i]};
}
sparse_table.init(
nodes,
[](const std::pair<int, int>& a, const std::pair<int, int>& b)
-> std::pair<int, int> {
return std::min(a, b);
});
}
int query(int u, int v) const {
u = this->left[u];
v = this->left[v];
if (u > v) std::swap(u, v);
return sparse_table.query(u, v + 1).second;
}
private:
SparseTable<std::pair<int, int>> sparse_table;
};
} // namespace emthrm
#endif // EMTHRM_GRAPH_TREE_LOWEST_COMMON_ANCESTOR_BY_EULER_TOUR_TECHNIQUE_HPP_
#line 1 "include/emthrm/graph/tree/lowest_common_ancestor_by_euler_tour_technique.hpp"
#include <algorithm>
#include <utility>
#include <vector>
#line 1 "include/emthrm/data_structure/sparse_table.hpp"
#line 5 "include/emthrm/data_structure/sparse_table.hpp"
#include <bit>
#include <cassert>
#include <functional>
#line 9 "include/emthrm/data_structure/sparse_table.hpp"
namespace emthrm {
template <typename Band>
struct SparseTable {
using BinOp = std::function<Band(Band, Band)>;
SparseTable() = default;
explicit SparseTable(const std::vector<Band>& a, const BinOp bin_op) {
init(a, bin_op);
}
void init(const std::vector<Band>& a, const BinOp bin_op_) {
bin_op = bin_op_;
const int n = a.size();
assert(n > 0);
lg.assign(n + 1, 0);
for (int i = 2; i <= n; ++i) {
lg[i] = lg[i >> 1] + 1;
}
const int table_h = std::countr_zero(std::bit_floor(a.size())) + 1;
data.assign(table_h, std::vector<Band>(n));
std::copy(a.begin(), a.end(), data.front().begin());
for (int i = 1; i < table_h; ++i) {
for (int j = 0; j + (1 << i) <= n; ++j) {
data[i][j] = bin_op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);
}
}
}
Band query(const int left, const int right) const {
assert(left < right);
const int h = lg[right - left];
return bin_op(data[h][left], data[h][right - (1 << h)]);
}
private:
BinOp bin_op;
std::vector<int> lg;
std::vector<std::vector<Band>> data;
};
} // namespace emthrm
#line 1 "include/emthrm/graph/edge.hpp"
/**
* @title 辺
*/
#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_
#include <compare>
namespace emthrm {
template <typename CostType>
struct Edge {
CostType cost;
int src, dst;
explicit Edge(const int src, const int dst, const CostType cost = 0)
: cost(cost), src(src), dst(dst) {}
auto operator<=>(const Edge& x) const = default;
};
} // namespace emthrm
#endif // EMTHRM_GRAPH_EDGE_HPP_
#line 1 "include/emthrm/graph/tree/euler_tour_technique.hpp"
#line 5 "include/emthrm/graph/tree/euler_tour_technique.hpp"
#line 1 "include/emthrm/graph/edge.hpp"
/**
* @title 辺
*/
#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_
#include <compare>
namespace emthrm {
template <typename CostType>
struct Edge {
CostType cost;
int src, dst;
explicit Edge(const int src, const int dst, const CostType cost = 0)
: cost(cost), src(src), dst(dst) {}
auto operator<=>(const Edge& x) const = default;
};
} // namespace emthrm
#endif // EMTHRM_GRAPH_EDGE_HPP_
#line 7 "include/emthrm/graph/tree/euler_tour_technique.hpp"
namespace emthrm {
template <typename CostType>
struct EulerTourTechnique {
std::vector<int> preorder, depth, left, right, down, up;
std::vector<CostType> tour;
explicit EulerTourTechnique(
const std::vector<std::vector<Edge<CostType>>> &graph, const int root = 0)
: graph(graph) {
const int n = graph.size();
left.resize(n);
right.resize(n);
down.assign(n, -1);
up.assign(n, (n - 1) << 1);
dfs(-1, root, 0);
}
template <typename Fn>
void update_v(const int ver, const Fn f) const {
f(left[ver], right[ver] + 1);
}
template <typename T, typename Fn>
T query_v(const int ver, const Fn f) const {
return f(left[ver], right[ver] + 1);
}
template <typename T, typename Fn>
T query_e(const int u, const int v, const Fn f) const {
return f(down[u] + 1, down[v] + 1);
}
template <typename Fn>
void update_subtree_e(const int ver, const Fn f) const {
f(down[ver] + 1, up[ver]);
}
template <typename T, typename Fn>
T query_subtree_e(const int ver, const Fn f) const {
return f(down[ver] + 1, up[ver]);
}
private:
const std::vector<std::vector<Edge<CostType>>> graph;
void dfs(const int par, const int ver, const int cur_depth) {
left[ver] = preorder.size();
preorder.emplace_back(ver);
depth.emplace_back(cur_depth);
for (const Edge<CostType>& e : graph[ver]) {
if (e.dst != par) {
down[e.dst] = tour.size();
tour.emplace_back(e.cost);
dfs(ver, e.dst, cur_depth + 1);
preorder.emplace_back(ver);
depth.emplace_back(cur_depth);
up[e.dst] = tour.size();
tour.emplace_back(-e.cost);
}
}
right[ver] = preorder.size() - 1;
}
};
} // namespace emthrm
#line 11 "include/emthrm/graph/tree/lowest_common_ancestor_by_euler_tour_technique.hpp"
namespace emthrm {
template <typename CostType>
struct LowestCommonAncestor : EulerTourTechnique<CostType> {
explicit LowestCommonAncestor(
const std::vector<std::vector<Edge<CostType>>>& graph,
const int root = 0)
: EulerTourTechnique<CostType>(graph, root) {
const int n = this->preorder.size();
std::vector<std::pair<int, int>> nodes(n);
for (int i = 0; i < n; ++i) {
nodes[i] = {this->depth[i], this->preorder[i]};
}
sparse_table.init(
nodes,
[](const std::pair<int, int>& a, const std::pair<int, int>& b)
-> std::pair<int, int> {
return std::min(a, b);
});
}
int query(int u, int v) const {
u = this->left[u];
v = this->left[v];
if (u > v) std::swap(u, v);
return sparse_table.query(u, v + 1).second;
}
private:
SparseTable<std::pair<int, int>> sparse_table;
};
} // namespace emthrm