-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCA.cpp
More file actions
63 lines (58 loc) · 1.41 KB
/
LCA.cpp
File metadata and controls
63 lines (58 loc) · 1.41 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
#include <bits/stdc++.h>
using namespace std;
// https://cses.fi/problemset/task/1135/
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(0);
#define int long long int
int m;
void dfs(int node, int par, vector<int> adj[], vector<int> &lvl, vector<vector<int>> &dp){
lvl[node] = lvl[par] + 1;
dp[node][0] = par;
for(int i=1;i<=m;i++){
dp[node][i] = dp[dp[node][i-1]][i-1];
}
for(auto ch:adj[node]){
if(ch != par){
dfs(ch, node, adj, lvl, dp);
}
}
}
int lca(int a, int b, vector<int> &lvl, vector<vector<int>> &dp){
if(lvl[a] < lvl[b]) swap(a, b);
int diff = lvl[a] - lvl[b];
for(int i=0;i<=m;i++){
if((diff>>i)&1){
a = dp[a][i];
}
}
if(a == b) return a;
for(int i=m;i>=0;i--){
if(dp[a][i] != dp[b][i]){
a = dp[a][i];
b = dp[b][i];
}
}
return dp[a][0];
}
int32_t main(){
FIO;
int n,q;
cin>>n>>q;
vector<int> adj[n+1];
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
m = ceil(log2(n));
vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
vector<int> lvl(n+1, 0);
dfs(1, 0, adj, lvl, dp);
while(q--){
int a,b;
cin>>a>>b;
int x = lvl[a] + lvl[b] - 2*lvl[lca(a, b, lvl, dp)];
cout<<x<<'\n';
}
return 0;
}