-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLcaBinaryLifting.h
50 lines (42 loc) · 1.01 KB
/
LcaBinaryLifting.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
#pragma once
#include <vector>
#include <functional>
#include <cmath>
#include "Graph/Graph.h"
class LcaBinaryLifting {
private:
std::vector<std::vector<int>> up;
std::vector<int> pre, post;
int maxLog;
bool isAncestor(int a, int b) {
return pre[a] <= pre[b] && post[a] >= post[b];
}
public:
template<typename L>
LcaBinaryLifting(const AdjList<L>& g, int root) : pre(g.size()), post(g.size()) {
maxLog = int(log2(int(g.size())));
up.assign(g.size(), std::vector<int>(maxLog + 1));
int count = 0;
std::function<void(int, int)> dfs = [&dfs, &count, &g, this](int u, int parent) {
pre[u] = count++;
up[u][0] = parent;
for (int d = 1; d <= maxLog; ++d)
up[u][d] = up[up[u][d - 1]][d - 1];
for (Edge<L> v : g.adj[u])
dfs(v.to, u);
post[u] = count++;
};
dfs(root, root);
}
int query(int a, int b) {
if (isAncestor(a, b))
return a;
if (isAncestor(b, a))
return b;
for (int d = maxLog; d >= 0; --d) {
if (!isAncestor(up[a][d], b))
a = up[a][d];
}
return up[a][0];
}
};