Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C++ code for lowest common ancestor problem solution using binary lifting #6786

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link

@gargantuadev gargantuadev Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm wrong, but I think we are not considering here the case in which k is the root node.

The change I would do is:

if (p == 0) { depth[k] = 0; // Root node ancestor[k][0] = k; // Root node's ancestor is itself } else { depth[k] = depth[p] + 1; ancestor[k][0] = p; }

What do you think @berkay-top ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary because the default values for arrays are already zero.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes completely agree, I missed the implicit initialization, nice job by the way!

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';
}
}