-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLCA.cpp
More file actions
79 lines (76 loc) · 1.54 KB
/
LCA.cpp
File metadata and controls
79 lines (76 loc) · 1.54 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
// Lowest Common Ancestor (binary lifting)
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int mod = 1e9 + 7;
int pa[200005][20];
vector<int>g[200005];
int dep[200005];
void dfs(int now, int pre){
pa[now][0] = pre;
for(auto i:g[now]){
if (i == pre) {
continue;
}
dep[i] = dep[now] + 1;
dfs(i, now);
}
}
int lca(int x,int y){
if(dep[x] < dep[y]){
swap(x,y);
}
int left = dep[x] - dep[y];
for(int i=19;i>=0;i--){
if(left >= (1<<i)){
x = pa[x][i];
left -= (1<<i);
}
}
if(x==y)return x;
for(int i=19;i>=0;i--){
if(pa[x][i]!=pa[y][i]){
x = pa[x][i];
y = pa[y][i];
}
}
return pa[x][0];
}
int dis(int x,int y) {
return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}
int getpa(int x, int k) {
int cur = x;
int i = 0;
while (k) {
if (k & 1) {
cur = pa[cur][i];
}
k >>= 1;
i++;
}
return cur;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0 ; i < n - 1 ; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,1);
for(int i = 1 ; i <= 19 ; i++){
for (int j = 1 ; j <= n ; j++){
pa[j][i] = pa[pa[j][i-1]][i-1];
}
}
int x, y;
cin >> x >> y;
int L = lca(x, y) // lca
int D = dis(x, y) // dis
int pa = getpa(x, y) // x's y time parent
}