![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Data structure for finding the lowest common ancestor of two vertices in a rooted tree using binary lifting. More...
#include <cassert>#include <iostream>#include <queue>#include <utility>#include <vector>Go to the source code of this file.
Classes | |
| class | graph::Graph< T > |
| class | graph::RootedTree |
| class | graph::LowestCommonAncestor |
Namespaces | |
| namespace | graph |
| Graph Algorithms. | |
Functions | |
| static void | tests () |
| int | main () |
Data structure for finding the lowest common ancestor of two vertices in a rooted tree using binary lifting.
Algorithm: https://cp-algorithms.com/graph/lca_binary_lifting.html
Complexity:
Example:
Tree:
_ 3 _
/ | \
1 6 4
/ | / \ \
7 5 2 8 0
|
9
lowest_common_ancestor(7, 4) = 3
lowest_common_ancestor(9, 6) = 6
lowest_common_ancestor(0, 0) = 0
lowest_common_ancestor(8, 2) = 6
The query is symmetrical, therefore lowest_common_ancestor(x, y) = lowest_common_ancestor(y, x)
Definition in file lowest_common_ancestor.cpp.
| int main | ( | void | ) |
Main function
Definition at line 255 of file lowest_common_ancestor.cpp.
|
static |
Unit tests
_ 3 _ / | \ 1 6 4
/ | / \ \ 7 5 2 8 0 | 9
Definition at line 234 of file lowest_common_ancestor.cpp.