-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathbinary_lifting.cpp
112 lines (96 loc) · 1.92 KB
/
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//Binary Lifting algorithm
//Find the Lowest common ancestor (LCA) using binary lifting approach
//Preprocess: O(nlogn), Query: O(logn)
#include<bits/stdc++.h>
#define endl "\n"
#define ll long long int
#define N (ll)(1e9+7)
#define T int
#define P pair<T,T>
#define umap unordered_map<T,T>
#define uset unordered_set<T>
#define rep(i,n) for(i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define print(v) for(auto x:v) cout<<x<< " "
#define pb push_back
using namespace std;
const int M=1e5+5;
const int lgn=20;
int level[M],par[M];
int dp[M][lgn+1];
vector<int> adj[M];
//run dfs and find the levels of all the vertices
void dfs(int u,int par,int d)
{
level[u]=d;
for(auto x:adj[u])
{
if(x!=par)
dfs(x,u,d+1);
}
}
/*** preprocess for binary lifting algo *****/
//Runtime: O(nlogn)
void preprocess(int n)
{
int i,j;
memset(dp,-1,sizeof(dp));
for(i=0;i<n;i++)
dp[i][0]=par[i];
for(j=1;j<=lgn;j++)
{
for(i=0;i<n;i++)
{
if(dp[i][j-1]!=-1)
dp[i][j]=dp[dp[i][j-1]][j-1];
}
}
}
//find the LCA using binary lifting
//Runtime: O(logn)
int lca(int a,int b)
{
if(level[a]<level[b])
swap(a,b);
int i;
for(i=lgn;i>=0;i--)
{
if(level[a]-(1<<i)>=level[b])
{
a=dp[a][i];
}
}
if(a==b)
return a;
for(i=lgn;i>=0;i--)
{
if(dp[a][i]!=-1 && dp[a][i]!=dp[b][i])
{
a=dp[a][i];
b=dp[b][i];
}
}
return par[a];
}
//driver code
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; //number of vertices of the tree
cin>>n;
int i,j;
//(n-1) edges of the tree
rep(i,n-1)
{
cin>>par[i+1];
adj[i+1].pb(par[i+1]);
adj[par[i+1]].pb(i+1);
}
preprocess(n);
dfs(0,-1,0);
int u,v; //we have to find the LCA of vertices u and v
cin>>u>>v;
cout<<lca(u,v)<<endl;
return 0;
}