This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub kmyk/competitive-programming-library
#include "graph/virtual_trees.hpp"
#pragma once
#include <vector>
#include "../utils/macros.hpp"
#include "../graph/euler_tour_preorder.hpp"
#include "../graph/lowest_common_ancestor.hpp"
/**
* @brief 虚樹 / virtual trees
* @note O(\lvert X \rvert \log \lvert X \rvert)
* @return the list of vertices in the virtual tree, and the adjacent list using indices on the returned list of vertices
* @note verified at https://codeforces.com/problemset/problem/613/D
*/
std::pair<std::vector<int>, std::vector<std::vector<int>>> construct_virtual_tree(const std::vector<std::vector<int>>& g, int root, const lowest_common_ancestor& lca, const std::vector<int>& tour_left, std::vector<int> marked) {
std::vector<int> vertices;
std::vector<std::vector<int>> h;
std::vector<int> stk;
std::sort(ALL(marked), [&](int x, int y) -> bool {
return tour_left[x] < tour_left[y];
});
for (int x : marked) {
int index_x = vertices.size();
vertices.push_back(x);
h.emplace_back();
if (not stk.empty()) {
int z = lca(x, vertices[stk.back()]);
// pop vectirces deeper than z
std::vector<int> popped;
while (not stk.empty() and lca.get_depth(z) < lca.get_depth(vertices[stk.back()])) {
int index_y = stk.back();
stk.pop_back();
popped.push_back(index_y);
}
// add the LCA if required
if (stk.empty() or vertices[stk.back()] != z) {
int index_z = vertices.size();
vertices.push_back(z);
h.emplace_back();
stk.push_back(index_z);
}
// add edges
if (not popped.empty()) {
int index_z = stk.back();
int index_y = popped.back();
h[index_z].push_back(index_y);
h[index_y].push_back(index_z);
}
REP (i, (int)popped.size() - 1) {
int index_y1 = popped[i];
int index_y2 = popped[i + 1];
h[index_y1].push_back(index_y2);
h[index_y2].push_back(index_y1);
}
}
stk.push_back(index_x);
}
REP (i, (int)stk.size() - 1) {
int index_x = stk[i];
int index_y = stk[i + 1];
h[index_x].push_back(index_y);
h[index_y].push_back(index_x);
}
return {vertices, h};
}
/**
* @note O(N log N + \lvert X \rvert \log \lvert X \rvert)
*/
std::pair<std::vector<int>, std::vector<std::vector<int>>> construct_virtual_tree(const std::vector<std::vector<int>>& g, int root, const std::vector<int>& marked) {
lowest_common_ancestor lca(root, g);
std::vector<int> tour, left, right;
do_euler_tour_preorder(g, root, tour, left, right);
return construct_virtual_tree(g, root, lca, left, marked);
}
#line 2 "graph/virtual_trees.hpp"
#include <vector>
#line 2 "utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 2 "graph/euler_tour_preorder.hpp"
#include <functional>
#line 4 "graph/euler_tour_preorder.hpp"
/**
* @brief Euler Tour (preorder)
* @arg g must be a rooted tree, directed or undirected
*/
void do_euler_tour_preorder(std::vector<std::vector<int> > const & g, int root, std::vector<int> & tour, std::vector<int> & left, std::vector<int> & right) {
int n = g.size();
tour.clear();
left.assign(n, -1);
right.assign(n, -1);
std::function<void (int, int)> go = [&](int x, int parent) {
left[x] = tour.size();
tour.push_back(x);
for (int y : g[x]) if (y != parent) {
go(y, x);
}
right[x] = tour.size();
};
go(root, -1);
}
#line 2 "graph/lowest_common_ancestor.hpp"
#include <algorithm>
#include <cassert>
#line 5 "graph/lowest_common_ancestor.hpp"
#include <utility>
#line 5 "data_structure/sparse_table.hpp"
/**
* @brief Sparse Table (idempotent monoid)
* @note the unit is required just for convenience
* @note $O(N \log N)$ space
*/
template <class IdempotentMonoid>
struct sparse_table {
typedef typename IdempotentMonoid::value_type value_type;
std::vector<std::vector<value_type> > table;
IdempotentMonoid mon;
sparse_table() = default;
/**
* @note $O(N \log N)$ time
*/
template <class InputIterator>
sparse_table(InputIterator first, InputIterator last, const IdempotentMonoid & mon_ = IdempotentMonoid())
: mon(mon_) {
table.emplace_back(first, last);
int n = table[0].size();
int log_n = 32 - __builtin_clz(n);
table.resize(log_n, std::vector<value_type>(n));
REP (k, log_n - 1) {
REP (i, n) {
table[k + 1][i] = i + (1ll << k) < n ?
mon.mult(table[k][i], table[k][i + (1ll << k)]) :
table[k][i];
}
}
}
/**
* @note $O(1)$
*/
value_type range_get(int l, int r) const {
if (l == r) return mon.unit(); // if there is no unit, remove this line
assert (0 <= l and l < r and r <= (int)table[0].size());
int k = 31 - __builtin_clz(r - l); // log2
return mon.mult(table[k][l], table[k][r - (1ll << k)]);
}
};
#line 3 "monoids/min_index.hpp"
#include <climits>
#include <limits>
#line 6 "monoids/min_index.hpp"
/**
* @note a semilattice
*/
template <class T>
struct min_index_monoid {
typedef std::pair<T, int> value_type;
value_type unit() const { return std::make_pair(std::numeric_limits<T>::max(), INT_MAX); }
value_type mult(value_type a, value_type b) const { return std::min(a, b); }
};
#line 9 "graph/lowest_common_ancestor.hpp"
/**
* @brief lowest common ancestor / 最小共通祖先 (前処理 $O(N)$ + $O(1)$, $\pm 1$ RMQ and sparse table)
* @see https://www.slideshare.net/yumainoue965/lca-and-rmq
* @note verified http://www.utpc.jp/2011/problems/travel.html
*/
struct lowest_common_ancestor {
sparse_table<min_index_monoid<int> > table;
std::vector<int> index;
lowest_common_ancestor() = default;
/**
* @note $O(N)$
* @param g is an adjacent list of a tree
* @note you can easily modify this to accept forests
*/
lowest_common_ancestor(int root, std::vector<std::vector<int> > const & g) {
std::vector<std::pair<int, int> > tour;
index.assign(g.size(), -1);
dfs(root, -1, 0, g, tour);
table = sparse_table<min_index_monoid<int> >(ALL(tour));
}
private:
/**
* @note sometimes causes stack overflow without ulimit -s unlimited
*/
void dfs(int x, int parent, int depth, std::vector<std::vector<int> > const & g, std::vector<std::pair<int, int> > & tour) {
index[x] = tour.size();
tour.emplace_back(depth, x);
for (int y : g[x]) if (y != parent) {
dfs(y, x, depth + 1, g, tour);
tour.emplace_back(depth, x);
}
}
public:
/**
* @note $O(1)$
*/
int operator () (int x, int y) const {
assert (0 <= x and x < index.size());
assert (0 <= y and y < index.size());
x = index[x];
y = index[y];
if (x > y) std::swap(x, y);
return table.range_get(x, y + 1).second;
}
int get_depth(int x) const {
assert (0 <= x and x < index.size());
return table.range_get(index[x], index[x] + 1).first;
}
int get_dist(int x, int y) const {
assert (0 <= x and x < index.size());
assert (0 <= y and y < index.size());
int z = (*this)(x, y);
return get_depth(x) + get_depth(y) - 2 * get_depth(z);
}
};
#line 6 "graph/virtual_trees.hpp"
/**
* @brief 虚樹 / virtual trees
* @note O(\lvert X \rvert \log \lvert X \rvert)
* @return the list of vertices in the virtual tree, and the adjacent list using indices on the returned list of vertices
* @note verified at https://codeforces.com/problemset/problem/613/D
*/
std::pair<std::vector<int>, std::vector<std::vector<int>>> construct_virtual_tree(const std::vector<std::vector<int>>& g, int root, const lowest_common_ancestor& lca, const std::vector<int>& tour_left, std::vector<int> marked) {
std::vector<int> vertices;
std::vector<std::vector<int>> h;
std::vector<int> stk;
std::sort(ALL(marked), [&](int x, int y) -> bool {
return tour_left[x] < tour_left[y];
});
for (int x : marked) {
int index_x = vertices.size();
vertices.push_back(x);
h.emplace_back();
if (not stk.empty()) {
int z = lca(x, vertices[stk.back()]);
// pop vectirces deeper than z
std::vector<int> popped;
while (not stk.empty() and lca.get_depth(z) < lca.get_depth(vertices[stk.back()])) {
int index_y = stk.back();
stk.pop_back();
popped.push_back(index_y);
}
// add the LCA if required
if (stk.empty() or vertices[stk.back()] != z) {
int index_z = vertices.size();
vertices.push_back(z);
h.emplace_back();
stk.push_back(index_z);
}
// add edges
if (not popped.empty()) {
int index_z = stk.back();
int index_y = popped.back();
h[index_z].push_back(index_y);
h[index_y].push_back(index_z);
}
REP (i, (int)popped.size() - 1) {
int index_y1 = popped[i];
int index_y2 = popped[i + 1];
h[index_y1].push_back(index_y2);
h[index_y2].push_back(index_y1);
}
}
stk.push_back(index_x);
}
REP (i, (int)stk.size() - 1) {
int index_x = stk[i];
int index_y = stk[i + 1];
h[index_x].push_back(index_y);
h[index_y].push_back(index_x);
}
return {vertices, h};
}
/**
* @note O(N log N + \lvert X \rvert \log \lvert X \rvert)
*/
std::pair<std::vector<int>, std::vector<std::vector<int>>> construct_virtual_tree(const std::vector<std::vector<int>>& g, int root, const std::vector<int>& marked) {
lowest_common_ancestor lca(root, g);
std::vector<int> tour, left, right;
do_euler_tour_preorder(g, root, tour, left, right);
return construct_virtual_tree(g, root, lca, left, marked);
}