-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6786 from berkay-top/lowest-common-ancestor
Added C++ code for lowest common ancestor problem solution using binary lifting
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
code/graph_algorithms/src/lowest_common_ancestor/lowest_common_ancestor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <array> | ||
using namespace std; | ||
|
||
// Part of Cosmos by OpenGenus Foundation | ||
|
||
// Problem Statement | ||
/* | ||
* Given a tree with n nodes | ||
* Your task is to process q queries of the following form: | ||
* who is the lowest common ancestor of nodes a and b in the tree? | ||
* | ||
* The lowest common ancestor of two nodes of a rooted tree is the lowest node | ||
* whose subtree contains both the nodes. | ||
*/ | ||
|
||
void dfs(int k, int p, vector<int> &depth, vector<vector<int>> &adj, vector<array<int, 20>> &ancestor) | ||
{ | ||
ancestor[k][0] = p; | ||
depth[k] = depth[p] + 1; | ||
for (int i = 1; i < 20; i++) | ||
ancestor[k][i] = ancestor[ancestor[k][i - 1]][i - 1]; | ||
|
||
for (int i : adj[k]) | ||
if (i != p) | ||
dfs(i, k, depth, adj, ancestor); | ||
} | ||
|
||
int kthAncestor(int k, int x, vector<array<int, 20>> &ancestor) | ||
{ | ||
for (int i = 0; i < 20; i++) | ||
if (x & (1 << i)) | ||
k = ancestor[k][i]; | ||
|
||
return k; | ||
} | ||
|
||
int lca(int a, int b, vector<array<int, 20>> &ancestor, vector<int> &depth) | ||
{ | ||
if (depth[a] < depth[b]) | ||
swap(a, b); | ||
|
||
a = kthAncestor(a, depth[a] - depth[b], ancestor); | ||
|
||
if (a == b) | ||
return b; | ||
|
||
for (int i = 19; i >= 0; i--) | ||
if (ancestor[a][i] != ancestor[b][i]) | ||
a = ancestor[a][i], b = ancestor[b][i]; | ||
|
||
return ancestor[a][0]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<vector<int>> adj(n + 1); | ||
for (int i = 1; i < n; i++) | ||
{ | ||
int a, b; | ||
cin >> a >> b; | ||
adj[a].push_back(b); | ||
adj[b].push_back(a); | ||
} | ||
|
||
vector<int> depth(n + 1); | ||
vector<array<int, 20>> ancestor(n + 1); | ||
|
||
dfs(1, 0, depth, adj, ancestor); | ||
|
||
int q; | ||
cin >> q; | ||
while (q--) | ||
{ | ||
int a, b; | ||
cin >> a >> b; | ||
cout << lca(a, b, ancestor, depth) << '\n'; | ||
} | ||
} |