-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathLCA (Binary Lifting).cpp
71 lines (60 loc) · 1.53 KB
/
LCA (Binary Lifting).cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
// Time complexity: O(nlogn) build, O(logn) per query
// Problem link: https://cses.fi/problemset/task/1688
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 2e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const int MAX_L = 20;
int n, q, par[MAX_N][MAX_L], dep[MAX_N];
vector<int> adj[MAX_N];
void dfs(int u, int p = 0) {
par[u][0] = p;
for (int i = 1; i < MAX_L; i++)
par[u][i] = par[par[u][i - 1]][i - 1];
for (int v : adj[u]) {
if (v == p) continue;
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
int ancestor(int u, int k) {
for (int i = 0; i < MAX_L; i++)
if (k & (1 << i))
u = par[u][i];
return u;
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
u = ancestor(u, dep[u] - dep[v]);
if (u == v) return u;
for (int i = MAX_L - 1; i >= 0; i--)
if (par[u][i] != par[v][i])
u = par[u][i], v = par[v][i];
return par[u][0];
}
void solve() {
cin >> n >> q;
for (int v = 2; v <= n; v++) {
int u; cin >> u;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
while (q--) {
int u, v; cin >> u >> v;
cout << lca(u, v) << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}