-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCA.cpp
More file actions
87 lines (67 loc) · 1.78 KB
/
LCA.cpp
File metadata and controls
87 lines (67 loc) · 1.78 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
///Source: Folklore, Anachor
#include<bits/stdc++.h>
using namespace std;
namespace LCA{
const int N = 3e5+7, K = 20;
vector<int> adj[N];
int anc[N][K];
int level[N];
void setup(int u, int par) {
level[u] = level[par]+1;
anc[u][0] = par;
for (int k=1; k<K; k++) anc[u][k] = anc[anc[u][k-1]][k-1];
for (int v: adj[u]) {
if (v == par) continue;
setup(v, u);
}
}
int lca(int u, int v) {
if (level[u] > level[v]) swap(u, v);
for (int k=K-1; k>=0; k--)
if (level[u] + (1<<k) <= level[v]) v = anc[v][k];
if (u == v) return u;
for (int k=K-1; k>=0; k--)
if (anc[u][k] != anc[v][k])
u = anc[u][k], v = anc[v][k];
return anc[u][0];
}
int getanc(int u, int d) {
for (int k=0; k<K; k++)
if (d & (1<<k))
u = anc[u][k];
return u;
}
int dist(int u, int v) {
int g = lca(u, v);
return level[u] + level[v] - 2*level[g];
}
}
using namespace LCA;
///Given u, v, d per query find dth node (0 indexed) on path from u to v.
///https://codeforces.com/gym/102694/problem/C
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for (int i=1; i<n; i++) {
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
setup(1, 0);
int q;
cin>>q;
while (q--) {
int u, v, d, ans;
cin>>u>>v>>d;
int g = lca(u, v);
int ll = level[u] - level[g];
int rr = level[v] - level[g];
if (d >= ll + rr) ans = v;
else if (d <= ll) ans = getanc(u, d);
else ans = getanc(v, ll+rr-d);
cout<<ans<<"\n";
}
}