-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathlca.cpp
More file actions
88 lines (67 loc) · 1.58 KB
/
lca.cpp
File metadata and controls
88 lines (67 loc) · 1.58 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
// Taken from striver_79's github repo
// Stores the parent of every node
int par[N][21];
// Stores the dept
int dep[N];
// Stores the tree
vector<int> g[N];
// Function to pre-compute DFS
void dfs(int v, int p = 0)
{
// First parent
par[v][0] = p;
// Finds the parent at 2^i
for (int i = 1; i <= 20; i++)
par[v][i] = par[par[v][i - 1]][i - 1];
// Call DFS function
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
// Already visited
if (to == p)
continue;
// Find the depth
dep[to] = dep[v] + 1;
// Call the Dfs Function
dfs(to, v);
}
}
// Function to find the lca of a and b
int lca(int a, int b)
{
// If a is higher than b then swap them
// Tree me a niche rahegi and b upar hamesha
if (dep[a] < dep[b])
swap(a, b);
// Make both of them at equal height
for (int i = 20; i >= 0; i--)
{
if (dep[a] - (1 << i) >= dep[b])
a = par[a][i];
}
// Now traverse till up
// till we donot get parent same
for (int i = 20; i >= 0; --i)
{
if (par[a][i] != par[b][i])
{
a = par[a][i],
b = par[b][i];
}
}
// Till now not same
if (a != b)
a = par[a][0];
return a;
}
// Function to find the distance between a and b
int solve(int a, int b)
{
if (a == b)
return 0;
if (dep[a] < dep[b])
swap(a, b);
// Find lca
int q = lca(a, b);
int dist = dep[a] + dep[b] - 2 * dep[q];
return dist;
}