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

Conversation

berkay-top
Copy link
Contributor

Fixes issue:

#6785

Changes:

I have added Lowest Common Ancestor problem solution with binary lifting in C++.


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!

Copy link

@gargantuadev gargantuadev left a comment

Choose a reason for hiding this comment

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

I wrote the comment about the changes, tell me what you think about it!

@AdiChat AdiChat merged commit 47fc66f into OpenGenus:master Oct 5, 2024
Copy link

@gargantuadev gargantuadev left a comment

Choose a reason for hiding this comment

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

Approved on my side!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants