-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowestCommonAncestorTree.cpp
More file actions
42 lines (42 loc) · 1.7 KB
/
LowestCommonAncestorTree.cpp
File metadata and controls
42 lines (42 loc) · 1.7 KB
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
struct LowestCommonAncestorTree {
const int LOGM = 30;
vector<int> depth;
vector<vector<int>> parent;
LowestCommonAncestorTree(int root, const vector<vector<int>> &g) {
int n = g.size();
depth.resize(n);
parent.resize(LOGM);
for (int i = 0; i < LOGM; i ++) parent[i].resize(n);
function<void (int, int, int)> dfs = [&](int u, int prev, int d) {
parent[0][u] = prev;
depth[u] = d;
for (auto v : g[u]) if (v != prev) dfs(v, u, d + 1);
};
dfs(root, -1, 0);
for (int k = 0; k < LOGM - 1; k ++) {
for (int i = 0; i < n; i ++) {
if (parent[k][i] < 0) parent[k + 1][i] = -1;
else parent[k + 1][i] = parent[k][parent[k][i]];
}
}
}
int lca(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
for (int k = 0; k < LOGM; k ++) {
if ((depth[v] - depth[u]) >> k & 1) {
v = parent[k][v];
}
}
if (u == v) return u;
for (int k = LOGM - 1; k >= 0; k --) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
};