競技プログラミング用 C++ ライブラリ
components/static_top_tree_structure.hpp · GitHub で見る
共通ページ
static_top_tree_detail::Structure は Static Top Tree のクラスタ構造を作る内部部品です。DP の値には依存しません。構築済みの HLD を渡します。
| 操作・メンバ | 内容・返り値 | 計算量 |
|---|---|---|
Structure() |
空の構造を作る | \(O(1)\) |
Structure(tree) / build(tree)
|
HLD から構築/再構築 | \(O(n\log n)\) |
Kind |
Vertex・Compress・Rake の 3 種類 |
—(型) |
Node |
parent・left・right・index・kind を持つクラスタ |
—(型) |
nodes |
クラスタ配列。先頭 n 個は頂点番号に対応する Vertex
|
\(O(1)\) |
order |
子から親への評価順。頂点番号順とは限らない | \(O(1)\) |
parent[v] |
元の木の親。根では -1
|
\(O(1)\) |
edge_child[e] |
入力した辺 e の子側の頂点 |
\(O(1)\) |
root / root_vertex
|
最終クラスタ/元の木の根。空なら -1
|
\(O(1)\) |
paths / points
|
Vertex・Compress の合計数/Rake の数 |
\(O(1)\) |
height |
クラスタ木の最大深さ。空・1 頂点では 0 | \(O(1)\) |
Vertex の left は light な子の集約で、存在しない場合は -1 です。right は使いません。Compress は上側・下側の順に、Rake は同じ親に接続する枝として left・right を併合します。parent はクラスタ木の親で、最終クラスタのみ -1 です。index は Vertex・Compress 用の配列、または Rake 用の配列に保存する際の添字です。
heavy path の連結は高さを使うスタック、light な枝の併合は高さの小さいクラスタを優先するヒープで平衡化します。構築・走査には再帰を使いません。
構築済みの HLD の頂点数を \(n\) として、構築は \(O(n\log n)\) 時間、空間は \(O(n)\)、クラスタ木の高さは \(O(\log n)\) です。
#ifndef STATIC_TOP_TREE_STRUCTURE_HPP
#define STATIC_TOP_TREE_STRUCTURE_HPP
#include <algorithm>
#include <cassert>
#include <functional>
#include <limits>
#include <utility>
#include <vector>
#include "../tree/hld.hpp"
namespace static_top_tree_detail {
struct Structure {
enum Kind : unsigned char { Vertex, Compress, Rake };
struct Node {
int parent = -1, left = -1, right = -1, index = -1;
Kind kind = Vertex;
};
std::vector<Node> nodes;
std::vector<int> order, parent, edge_child;
int root = -1, root_vertex = -1, paths = 0, points = 0, height = 0;
Structure() = default;
explicit Structure(const HLD& tree) { build(tree); }
void build(const HLD& tree) {
int n = tree.n;
assert(n <= std::numeric_limits<int>::max() / 2);
assert(int(tree.order.size()) == n);
nodes.assign(n, {}), order.clear();
nodes.reserve(std::max(0, 2 * n - 1)), order.reserve(std::max(0, 2 * n - 1));
parent = tree.parent;
edge_child.clear(), edge_child.reserve(tree.edges.size());
for (auto [u, v] : tree.edges) edge_child.push_back(parent[u] == v ? u : v);
paths = n, points = 0, root = -1, height = 0;
root_vertex = n ? tree.order[0] : -1;
for (int v = 0; v < n; ++v) nodes[v].index = v;
std::vector<std::pair<int, int>> heads(n), stack, heap;
auto join = [&](std::pair<int, int> a, std::pair<int, int> b, Kind kind) {
int id = int(nodes.size());
nodes.push_back({-1, a.second, b.second, kind == Compress ? paths++ : points++, kind});
nodes[a.second].parent = nodes[b.second].parent = id;
order.push_back(id);
return std::pair<int, int>{std::max(a.first, b.first) + 1, id};
};
auto merge = [&] {
auto b = stack.back(); stack.pop_back();
auto a = stack.back(); stack.back() = join(a, b, Compress);
};
for (int i = n - 1; i >= 0; --i) {
int head = tree.order[i];
if (tree.head[head] != head) continue;
stack.clear();
for (int v = head; v != -1; v = tree.heavy[v]) {
heap.clear();
for (int j = tree.in[v] + 1; j < tree.out[v];) {
int u = tree.order[j];
if (u != tree.heavy[v]) heap.push_back(heads[u]);
j = tree.out[u];
}
using Greater = std::greater<std::pair<int, int>>;
std::make_heap(heap.begin(), heap.end(), Greater{});
while (heap.size() > 1) {
std::pop_heap(heap.begin(), heap.end(), Greater{});
auto a = heap.back(); heap.pop_back();
std::pop_heap(heap.begin(), heap.end(), Greater{});
auto b = heap.back(); heap.back() = join(a, b, Rake);
std::push_heap(heap.begin(), heap.end(), Greater{});
}
int h = 0;
if (!heap.empty()) {
nodes[v].left = heap[0].second;
nodes[heap[0].second].parent = v;
h = heap[0].first + 1;
}
order.push_back(v);
stack.emplace_back(h, v);
while (true) {
int s = int(stack.size());
if (s >= 3 && (stack[s - 3].first == stack[s - 2].first || stack[s - 3].first <= stack[s - 1].first)) {
auto last = stack.back(); stack.pop_back();
merge(), stack.push_back(last);
} else if (s >= 2 && stack[s - 2].first <= stack[s - 1].first) merge();
else break;
}
}
while (stack.size() > 1) merge();
heads[head] = stack[0];
root = stack[0].second, height = stack[0].first;
}
}
};
}
#endif
#line 1 "components/static_top_tree_structure.hpp"
#include <algorithm>
#include <cassert>
#include <functional>
#include <limits>
#include <utility>
#include <vector>
#line 1 "tree/hld.hpp"
#line 5 "tree/hld.hpp"
#include <array>
#line 7 "tree/hld.hpp"
#include <cstddef>
#line 11 "tree/hld.hpp"
class HLD {
int shape_ = 0, center_ = 0;
struct Node { int position, head, parent, offset; };
std::vector<Node> nodes;
void check([[maybe_unused]] int v) const { assert(0 <= v && v < n && int(in.size()) == n); }
void initialize() {
depth.assign(n, 0);
size.assign(n, 1);
heavy.assign(n, -1);
head.resize(n), in.resize(n), out.resize(n), order.resize(n);
nodes.resize(n);
shape_ = center_ = 0;
}
void finish(int root) {
in[root] = 0, out[root] = n, head[root] = root;
for (int i = n - 2; i >= 0; --i) {
int v = order[i], p = parent[v];
depth[v] = depth[p] + 1;
if (heavy[p] == v) in[v] = in[p] + 1, head[v] = head[p];
else in[v] = out[p] -= size[v], head[v] = v;
out[v] = in[v] + size[v];
}
for (int v = 0; v < n; ++v) out[v] = in[v] + size[v], order[in[v]] = v;
parent[root] = -1;
for (int v = 0; v < n; ++v) nodes[v] = {in[v], in[head[v]], parent[head[v]], depth[v] - in[v]};
}
public:
struct Segment {
int l, r;
bool reversed;
};
int n;
std::vector<std::pair<int, int>> edges;
std::vector<int> parent, depth, size, heavy, head, in, out, order;
explicit HLD(int n = 0) : n(n) {
assert(0 <= n && n <= std::numeric_limits<int>::max() / 2);
edges.reserve(std::max(0, n - 1));
}
int add_edge(int u, int v) {
assert(0 <= u && u < n && 0 <= v && v < n);
edges.emplace_back(u, v);
return int(edges.size()) - 1;
}
void build(int root = 0) {
assert(n >= 0 && edges.size() == std::size_t(std::max(0, n - 1)));
assert(!n || (0 <= root && root < n));
parent.assign(n, -1);
initialize();
if (!n) return;
std::vector<int> degree(n), neighbors(n);
for (auto [u, v] : edges) {
++degree[u], ++degree[v];
neighbors[u] ^= v, neighbors[v] ^= u;
}
center_ = int(std::max_element(degree.begin(), degree.end()) - degree.begin());
shape_ = degree[center_] <= 2 ? 1 : degree[center_] == n - 1 ? 2 : 0;
degree[root] += 2;
int count = 0;
for (int v = 0; v < n; ++v) if (degree[v] == 1) order[count++] = v;
for (int i = 0; i < count; ++i) {
int v = order[i], p = neighbors[v];
parent[v] = p;
size[p] += size[v];
if (heavy[p] == -1 || size[heavy[p]] < size[v]) heavy[p] = v;
neighbors[p] ^= v;
if (--degree[p] == 1) order[count++] = p;
}
assert(count == n - 1);
finish(root);
}
void build_from_parents(std::vector<int> parents) {
assert(parents.size() <= std::size_t(std::numeric_limits<int>::max() / 2));
n = static_cast<int>(parents.size());
parent = std::move(parents);
initialize();
edges.clear();
edges.reserve(std::max(0, n - 1));
if (!n) return;
std::fill(out.begin(), out.end(), 0);
int root = -1;
bool ordered = true;
for (int v = 0; v < n; ++v) {
int p = parent[v];
assert(-1 <= p && p < n && p != v);
if (p == -1) {
assert(root == -1);
root = v;
} else {
++out[p];
edges.emplace_back(p, v);
ordered &= p < v;
}
}
assert(root != -1);
int maximum = 0;
for (int v = 0; v < n; ++v) {
int degree = out[v] + (v != root);
if (degree > maximum) maximum = degree, center_ = v;
}
shape_ = maximum <= 2 ? 1 : maximum == n - 1 ? 2 : 0;
if (ordered) {
for (int v = n - 1; v > 0; --v) {
int p = parent[v];
size[p] += size[v];
if (heavy[p] == -1 || size[heavy[p]] < size[v]) heavy[p] = v;
order[n - 1 - v] = v;
}
} else {
int count = 0;
for (int v = 0; v < n; ++v) if (!out[v] && v != root) order[count++] = v;
for (int i = 0; i < count; ++i) {
int v = order[i], p = parent[v];
size[p] += size[v];
if (heavy[p] == -1 || size[heavy[p]] < size[v]) heavy[p] = v;
if (!--out[p] && p != root) order[count++] = p;
}
assert(count == n - 1);
}
finish(root);
}
int lca(int u, int v) const {
check(u), check(v);
if (shape_ == 1) return head[u] != head[v] ? order[0] : in[u] < in[v] ? u : v;
if (shape_ == 2) return u == v ? u : u == order[0] || v == order[0] ? order[0] : center_;
while (nodes[u].head != nodes[v].head) {
if (nodes[u].position > nodes[v].position) u = nodes[u].parent;
else v = nodes[v].parent;
}
return nodes[u].position < nodes[v].position ? u : v;
}
int dist(int u, int v) const {
int w = lca(u, v);
return depth[u] + depth[v] - 2 * depth[w];
}
bool is_ancestor(int u, int v) const {
check(u), check(v);
return in[u] <= in[v] && in[v] < out[u];
}
int ancestor(int v, int k) const {
check(v);
if (shape_) {
if (k < 0 || k > depth[v]) return -1;
if (!k) return v;
if (k == depth[v]) return order[0];
return shape_ == 2 ? parent[v] : order[in[v] - k];
}
if (k < 0) return -1;
int target = nodes[v].position + nodes[v].offset - k;
if (target < 0) return -1;
while (nodes[v].head + nodes[v].offset > target) v = nodes[v].parent;
return order[target - nodes[v].offset];
}
int jump(int u, int v, int k) const {
check(u), check(v);
if (k < 0) return -1;
if (!k) return u;
if (shape_ == 1) {
if (head[u] == head[v]) {
int delta = in[v] - in[u];
if (k > (delta < 0 ? -delta : delta)) return -1;
return order[in[u] + (delta < 0 ? -k : k)];
}
int a = depth[u], b = depth[v];
if (k > a + b) return -1;
if (k == a) return order[0];
return k < a ? order[in[u] - k] : order[in[v] - (a + b - k)];
}
if (shape_ == 2) {
int d = (u != center_) + (v != center_);
if (u == v || k > d) return -1;
return k == d ? v : center_;
}
int from = u, to = v, target = nodes[u].position + nodes[u].offset - k;
while (nodes[u].head != nodes[v].head) {
if (nodes[u].position > nodes[v].position) {
if (nodes[u].head + nodes[u].offset <= target) return order[target - nodes[u].offset];
u = nodes[u].parent;
} else v = nodes[v].parent;
}
int w = nodes[u].position < nodes[v].position ? u : v;
int d = nodes[w].position + nodes[w].offset;
int length = nodes[from].position + nodes[from].offset + nodes[to].position + nodes[to].offset - 2 * d;
if (k > length) return -1;
if (target >= d) return order[target - nodes[u].offset];
return ancestor(to, length - k);
}
std::pair<int, int> subtree(int v, bool edge = false) const {
check(v);
return {in[v] + edge, out[v]};
}
int edge_vertex(int id) const {
assert(0 <= id && id < int(edges.size()));
auto [u, v] = edges[id];
check(u);
return parent[u] == v ? u : v;
}
template <class F>
void for_each_path(int u, int v, F &&f, bool edge = false) const {
check(u), check(v);
if (shape_ == 1) {
int x = in[u], y = in[v];
if (head[u] == head[v]) {
int l = std::min(x, y) + edge, r = std::max(x, y) + 1;
if (l < r) f(l, r, x > y);
} else if (head[u] == order[0]) {
if (edge < x + 1) f(int(edge), x + 1, true);
f(in[head[v]], y + 1, false);
} else {
f(in[head[u]], x + 1, true);
if (edge < y + 1) f(int(edge), y + 1, false);
}
return;
}
std::array<std::array<int, 2>, 32> right;
int count = 0;
while (nodes[u].head != nodes[v].head) {
if (nodes[u].position > nodes[v].position) {
f(nodes[u].head, nodes[u].position + 1, true);
u = nodes[u].parent;
} else {
assert(count < int(right.size()));
right[count++] = {nodes[v].head, nodes[v].position + 1};
v = nodes[v].parent;
}
}
bool reversed = nodes[u].position > nodes[v].position;
int l = std::min(nodes[u].position, nodes[v].position) + edge, r = std::max(nodes[u].position, nodes[v].position) + 1;
if (l < r) f(l, r, reversed);
while (count) {
auto [a, b] = right[--count];
f(a, b, false);
}
}
std::vector<Segment> path(int u, int v, bool edge = false) const {
std::vector<Segment> result;
for_each_path(u, v, [&](int l, int r, bool reversed) { result.push_back({l, r, reversed}); }, edge);
return result;
}
};
#line 11 "components/static_top_tree_structure.hpp"
namespace static_top_tree_detail {
struct Structure {
enum Kind : unsigned char { Vertex, Compress, Rake };
struct Node {
int parent = -1, left = -1, right = -1, index = -1;
Kind kind = Vertex;
};
std::vector<Node> nodes;
std::vector<int> order, parent, edge_child;
int root = -1, root_vertex = -1, paths = 0, points = 0, height = 0;
Structure() = default;
explicit Structure(const HLD& tree) { build(tree); }
void build(const HLD& tree) {
int n = tree.n;
assert(n <= std::numeric_limits<int>::max() / 2);
assert(int(tree.order.size()) == n);
nodes.assign(n, {}), order.clear();
nodes.reserve(std::max(0, 2 * n - 1)), order.reserve(std::max(0, 2 * n - 1));
parent = tree.parent;
edge_child.clear(), edge_child.reserve(tree.edges.size());
for (auto [u, v] : tree.edges) edge_child.push_back(parent[u] == v ? u : v);
paths = n, points = 0, root = -1, height = 0;
root_vertex = n ? tree.order[0] : -1;
for (int v = 0; v < n; ++v) nodes[v].index = v;
std::vector<std::pair<int, int>> heads(n), stack, heap;
auto join = [&](std::pair<int, int> a, std::pair<int, int> b, Kind kind) {
int id = int(nodes.size());
nodes.push_back({-1, a.second, b.second, kind == Compress ? paths++ : points++, kind});
nodes[a.second].parent = nodes[b.second].parent = id;
order.push_back(id);
return std::pair<int, int>{std::max(a.first, b.first) + 1, id};
};
auto merge = [&] {
auto b = stack.back(); stack.pop_back();
auto a = stack.back(); stack.back() = join(a, b, Compress);
};
for (int i = n - 1; i >= 0; --i) {
int head = tree.order[i];
if (tree.head[head] != head) continue;
stack.clear();
for (int v = head; v != -1; v = tree.heavy[v]) {
heap.clear();
for (int j = tree.in[v] + 1; j < tree.out[v];) {
int u = tree.order[j];
if (u != tree.heavy[v]) heap.push_back(heads[u]);
j = tree.out[u];
}
using Greater = std::greater<std::pair<int, int>>;
std::make_heap(heap.begin(), heap.end(), Greater{});
while (heap.size() > 1) {
std::pop_heap(heap.begin(), heap.end(), Greater{});
auto a = heap.back(); heap.pop_back();
std::pop_heap(heap.begin(), heap.end(), Greater{});
auto b = heap.back(); heap.back() = join(a, b, Rake);
std::push_heap(heap.begin(), heap.end(), Greater{});
}
int h = 0;
if (!heap.empty()) {
nodes[v].left = heap[0].second;
nodes[heap[0].second].parent = v;
h = heap[0].first + 1;
}
order.push_back(v);
stack.emplace_back(h, v);
while (true) {
int s = int(stack.size());
if (s >= 3 && (stack[s - 3].first == stack[s - 2].first || stack[s - 3].first <= stack[s - 1].first)) {
auto last = stack.back(); stack.pop_back();
merge(), stack.push_back(last);
} else if (s >= 2 && stack[s - 2].first <= stack[s - 1].first) merge();
else break;
}
}
while (stack.size() > 1) merge();
heads[head] = stack[0];
root = stack[0].second, height = stack[0].first;
}
}
};
}