-
Notifications
You must be signed in to change notification settings - Fork 3
/
heavy_light_decomp_base.h
51 lines (44 loc) · 1.21 KB
/
heavy_light_decomp_base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <algorithm>
#include <vector>
struct HeavyLightDecompositionBase {
using Tree = std::vector<std::vector<int>>;
explicit HeavyLightDecompositionBase(const Tree &tree, int root)
: n(tree.size()), parent(n), size(n), depth(n), top(n), bottom(n) {
parent[root] = -1;
build(tree, root);
for (int u = 0; u < n; ++u) {
top[u] = top[bottom[u]];
}
}
int lca(int a, int b) const {
while (bottom[a] != bottom[b]) {
if (get_lowest_depth(a) > get_lowest_depth(b)) {
a = top[a];
} else {
b = top[b];
}
}
return depth[a] < depth[b] ? a : b;
}
int n;
std::vector<int> parent, size, depth;
protected:
void build(const Tree &tree, int u) {
const int p = parent[u];
depth[u] = ~p ? depth[p] + 1 : 0;
size[u] = 1;
std::pair<int, int> candidate{0, u};
for (int v : tree[u]) {
if (v != p) {
parent[v] = u;
build(tree, v);
size[u] += size[v];
candidate = std::max(candidate, std::make_pair(size[v], bottom[v]));
}
}
bottom[u] = candidate.second;
top[bottom[u]] = p;
}
int get_lowest_depth(int u) const { return ~top[u] ? depth[top[u]] : -1; }
std::vector<int> top, bottom;
};