|
| LowestCommonAncestor (const RootedTree &tree_) |
| Stores the tree and precomputs "up lifts".
|
|
int | lowest_common_ancestor (int u, int v) const |
| Query the structure to find the lowest common ancestor. Assumes that the provided numbers are valid indices of vertices. Iterativelly modifies ("lifts") u an v until it finnds their lowest common ancestor.
|
|
|
const RootedTree & | tree |
|
std::vector< std::vector< int > > | up |
| for every vertex stores a list of its ancestors by powers of two For each vertex, the first element of the corresponding list contains the index of its parent. The i-th element of the list is an index of the (2^i)-th ancestor of the vertex.
|
|
A structure that holds a rooted tree and allow for effecient queries of the lowest common ancestor of two given vertices in the tree.
Definition at line 145 of file lowest_common_ancestor.cpp.
◆ LowestCommonAncestor()
graph::LowestCommonAncestor::LowestCommonAncestor |
( |
const RootedTree & | tree_ | ) |
|
|
inlineexplicit |
◆ lowest_common_ancestor()
int graph::LowestCommonAncestor::lowest_common_ancestor |
( |
int | u, |
|
|
int | v ) const |
|
inline |
Query the structure to find the lowest common ancestor. Assumes that the provided numbers are valid indices of vertices. Iterativelly modifies ("lifts") u an v until it finnds their lowest common ancestor.
- Parameters
-
u | index of one of the queried vertex |
v | index of the other queried vertex |
- Returns
- index of the vertex which is the lowet common ancestor of u and v
Definition at line 164 of file lowest_common_ancestor.cpp.
164 {
165
166 if (tree.level[v] > tree.level[u]) {
167 std::swap(u, v);
168 }
169
170
171 int level_diff = tree.level[u] - tree.level[v];
172 for (int i = 0; (1 << i) <= level_diff; ++i) {
173 if (level_diff & (1 << i)) {
175 }
176 }
177 assert(tree.level[u] == tree.level[v]);
178
179 if (u == v) {
180 return u;
181 }
182
183
184 for (
int i =
static_cast<int>(
up[u].size()) - 1; i >= 0; --i) {
185 if (
up[u][i] !=
up[v][i]) {
188 }
189 }
190
191
192
193
194 assert(
up[u][0] ==
up[v][0]);
196 }
std::vector< std::vector< int > > up
for every vertex stores a list of its ancestors by powers of two For each vertex, the first element o...
◆ populate_up()
void graph::LowestCommonAncestor::populate_up |
( |
| ) |
|
|
inlineprotected |
Populate the "up" structure. See above.
Definition at line 212 of file lowest_common_ancestor.cpp.
212 {
213 up.resize(tree.number_of_vertices());
214 for (int vertex = 0; vertex < tree.number_of_vertices(); ++vertex) {
215 up[vertex].push_back(tree.parent[vertex]);
216 }
217 for (int level = 0; (1 << level) < tree.number_of_vertices(); ++level) {
218 for (int vertex = 0; vertex < tree.number_of_vertices(); ++vertex) {
219
220
221
222 up[vertex].push_back(
up[
up[vertex][level]][level]);
223 }
224 }
225 }
◆ tree
const RootedTree& graph::LowestCommonAncestor::tree |
◆ up
std::vector<std::vector<int> > graph::LowestCommonAncestor::up |
for every vertex stores a list of its ancestors by powers of two For each vertex, the first element of the corresponding list contains the index of its parent. The i-th element of the list is an index of the (2^i)-th ancestor of the vertex.
Definition at line 206 of file lowest_common_ancestor.cpp.
The documentation for this class was generated from the following file: