(
size_tN,
conststd::vector<std::pair<int, int> > &undirected_edges) {
64 for(
auto&edge : undirected_edges) {
65 neighbors[edge.first].push_back(edge.second);
66 neighbors[edge.second].push_back(edge.first);
93 RootedTree(
conststd::vector<std::pair<int, int> > &undirected_edges,
95: Graph(undirected_edges.size() + 1, undirected_edges),
root(root_) {
124std::queue<int> queue_of_vertices;
125queue_of_vertices.push(
root);
126 while(!queue_of_vertices.empty()) {
127 intvertex = queue_of_vertices.front();
128queue_of_vertices.pop();
129 for(
intneighbor :
neighbors[vertex]) {
131 if(
parent[neighbor] == -1) {
132 parent[neighbor] = vertex;
134queue_of_vertices.push(neighbor);
166 if(tree.level[v] > tree.level[u]) {
171 intlevel_diff = tree.level[u] - tree.level[v];
172 for(
inti = 0; (1 << i) <= level_diff; ++i) {
173 if(level_diff & (1 << i)) {
177assert(tree.level[u] == tree.level[v]);
184 for(
inti =
static_cast<int>(
up[u].size()) - 1; i >= 0; --i) {
185 if(
up[u][i] !=
up[v][i]) {
194assert(
up[u][0] ==
up[v][0]);
206std::vector<std::vector<int> >
up;
213 up.resize(tree.number_of_vertices());
214 for(
intvertex = 0; vertex < tree.number_of_vertices(); ++vertex) {
215 up[vertex].push_back(tree.parent[vertex]);
217 for(
intlevel = 0; (1 << level) < tree.number_of_vertices(); ++level) {
218 for(
intvertex = 0; vertex < tree.number_of_vertices(); ++vertex) {
222 up[vertex].push_back(
up[
up[vertex][level]][level]);
244std::vector<std::pair<int, int> > edges = {
245{7, 1}, {1, 5}, {1, 3}, {3, 6}, {6, 2}, {2, 9}, {6, 8}, {4, 3}, {0, 4}};
std::vector< std::vector< int > > neighbors
for each vertex it stores a list indicies of its neighbors
Graph(size_t N, const std::vector< std::pair< int, int > > &undirected_edges)
Populate the adjacency list for each vertex in the graph. Assumes that evey edge is a pair of valid v...
int number_of_vertices() const
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...
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 i...
LowestCommonAncestor(const RootedTree &tree_)
Stores the tree and precomputs "up lifts".
std::vector< int > level
Stores the distance from the root.
std::vector< int > parent
Stores parent of every vertex and for root its own index. The root is technically not its own parent,...
RootedTree(const std::vector< std::pair< int, int > > &undirected_edges, int root_)
Constructs the tree by calculating parent for every vertex. Assumes a valid description of a tree is ...
int root
Index of the root vertex.
void populate_parents()
Calculate the parents for all the vertices in the tree. Implements the breadth first search algorithm...
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4