-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlca.cpp
More file actions
72 lines (66 loc) · 1.27 KB
/
lca.cpp
File metadata and controls
72 lines (66 loc) · 1.27 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
const int maxn=2e5+5;
const int logn=19;
int n;
vector<int> adj[maxn];
int dep[maxn];
int par[maxn][logn];
void dfs(int u,int dad) {
par[u][0]=dad;
for(auto nbr:adj[u])
{
if(nbr!=dad)
{
dep[nbr]=dep[u]+1;
dfs(nbr,u);
}
}
}
void build(int root=0) { // nodes 0 indexed : 0,1,..,n-1
dep[root]=0;
dfs(root,root);
for(int j=1;j<logn;j++){
for(int i=0;i<n;i++){
par[i][j]=par[par[i][j-1]][j-1];
}
}
}
int get_kp(int node,int k) {
for(int j=logn-1;j>=0;j--)
if(k&(1ll<<j))
node=par[node][j];
return node;
}
int lca(int x,int y) {
if(dep[x]<dep[y]) swap(x,y);
x= get_kp(x,dep[x]-dep[y]);
if(x==y) return x;
for(int j=logn-1;j>=0;j--){
if(par[x][j]!=par[y][j]){
x=par[x][j];
y=par[y][j];
}
}
return par[x][0];
}
int dist(int x,int y) {
int l=lca(x,y);
return dep[x]+dep[y]-2*dep[l];
}
void solve() {
int q;
cin>>n>>q;
for(int i=1;i<n;i++){
int val;
cin>>val;
--val;
adj[val].push_back(i);
adj[i].push_back(val);
}
build(0);
while(q--){
int x,y;
cin>>x>>y;
--x;--y;
cout<<lca(x,y)+1<<endl;
}
}