Repository navigation
Expand file tree
/
Copy pathLCA_Sparse.cpp
More file actions
108 lines (83 loc) · 2.46 KB
/
Copy pathLCA_Sparse.cpp
File metadata and controls
108 lines (83 loc) · 2.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <bits/stdc++.h>
using namespace std;
#define level 20
const int N=100005;
vector<int> Adj[N];
int depth[N];
bool visited[N];
int parent[N];
int dp[N][level];
void addEdge(int i,int j){
Adj[i].push_back(j);
Adj[j].push_back(i);
}
void dfs(int src){
for(auto it=Adj[src].begin();it!=Adj[src].end();it++){
int v=*it;
if(!visited[v]){
depth[v]=1+depth[src];
visited[v]=true;
parent[v]=src;
dfs(v);
}
}
return;
}
void preCompute(){
for(int i=1;i<N;i++){dp[i][0]=parent[i];} //*
for(int j=1;j<level;j++){
for(int i=1;i<N;i++){
if(dp[i][j-1]!=-1){
dp[i][j]=dp[dp[i][j-1]][j-1]; // 2^jth ancestor of i will be 2^(j-1)th ancestor of (2^(j-1)th ancestor of i)
}
}
}
return;
}
int LCA(int u,int v){
if(depth[u]>depth[v]){swap(u,v);}
int height=depth[v]-depth[u];
for(int i=0;i<=level;i++){
if((height>>i)&1){
v=dp[v][i]; // raising v to the same level as u
}
}
if(u==v){return u;}
for(int i=level-1;i>=0;i--){
if(dp[u][i]!=-1 && dp[u][i]!=dp[v][i]){ // let 2^kth ancestor be LCA(u,v). Ancestors above 2^kth ancestorlevel will also have the equality dp[u][i]==dp[v][i] true.
u=dp[u][i]; // so we are looking for the level where we hit dp[u][i]!=dp[v][i] first. After which we set u and v to dp[u][i] , dp[v][i]
v=dp[v][i]; // respectively. The outer loop ends with u & v being just below LCA.
}
}
return dp[u][0]; //*
}
void init(){
for(int i=0;i<N;i++){
visited[i]=false;
parent[i]=i;
depth[i]=0;
}
for(int i=0;i<N;i++){
for(int j=0;j<=level;j++){dp[i][j]=-1;}
}
return;
}
int main() {
addEdge(1,2);
addEdge(1,3);
addEdge(2,4);
addEdge(2,5);
addEdge(2,6);
addEdge(3,7);
addEdge(3,8);
init();
int src=1;
depth[src]++; //*
parent[src]=-1;
visited[src]=true;
dfs(src); // to compute depth and parent
preCompute();
cout<<LCA(4,7)<<"\n";
cout<<LCA(4,6)<<"\n";
return 0;
}