-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowest_common_ancestor.hpp
58 lines (51 loc) · 1.69 KB
/
lowest_common_ancestor.hpp
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
52
53
54
55
56
57
58
#pragma once
#include "graph/graph_template.hpp"
template <class T> struct LowestCommonAncestor {
std::vector<int> depth;
std::vector<std::vector<int>> parent;
int n, LOG;
LowestCommonAncestor(const Graph<T> &G, int root = 0) : n(int(G.size())), LOG(32 - __builtin_clz(n)) {
depth.assign(n, 0);
parent.assign(LOG, std::vector<int>(n));
auto dfs = [&](auto f, int cur, int par) -> void {
parent[0][cur] = par;
for (auto &e : G[cur]) {
if (e.to == par) continue;
depth[e.to] = depth[cur] + 1;
f(f, e.to, cur);
}
};
dfs(dfs, root, -1);
for (int k = 0; k + 1 < LOG; k++) {
for (int v = 0; v < n; v++) {
parent[k + 1][v] = (parent[k][v] < 0 ? -1 : parent[k][parent[k][v]]);
}
}
}
int lca(int u, int v) {
assert((int)depth.size() == n);
if (depth[u] > depth[v]) std::swap(u, v);
// depth[u] <= depth[v]
for (int k = 0; k < LOG; k++)
if ((depth[v] - depth[u]) >> k & 1) v = parent[k][v];
if (u == v) return u;
for (int k = LOG - 1; k >= 0; k--) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
int level_ancestor(int u, int d) {
assert((int)depth.size() == n);
if (depth[u] < d) return -1;
for (int k = 0; k < LOG; k++)
if (d >> k & 1) u = parent[k][u];
return u;
}
int distance(int u, int v) {
int par = lca(u, v);
return depth[u] + depth[v] - 2 * depth[par];
}
};