This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "graph/tree/virtual-tree.hpp"
wiki: https://oi-wiki.org/graph/virtual-tree/
Virtual tree only connects the edge for the important points without losing the position retation of these important points.
I implement the method 1 in the wiki.
(todo: Not verified)
#include "../graph-template.hpp"
#include "./doubling-lowest-common-ancestor.hpp"
template <typename T>
struct VirtualTree {
public:
VirtualTree() {}
Graph<T> build(const Graph<T>& g, const vector<int>& points, int root = 0) {
DoublingLowestCommonAncestor<T> LCA = DoublingLowestCommonAncestor(g, root);
timestamp = 0;
int n = g.size();
stt.resize(n);
dfs(root, -1);
auto p = points;
sort(p.begin(), p.end(), [&](int x, int y) {
return stt[x] < stt[y];
});
int sz = points.size();
for (int i = 0; i < sz - 1; i++) {
p.push_back(LCA.lca(p[i], p[i + 1]));
}
sort(p.begin(), p.end(), [&](int x, int y) {
return stt[x] < stt[y];
});
p.resize(unique(p.begin(), p.end()) - p.begin());
sz = p.size();
Graph<T> g2(n);
for (int i = 0; i < sz - 1; i++) {
int lc = LCA.lca(p[i], p[i + 1]);
g2.add_edge(lc, p[i + 1]);
}
return g2;
}
private:
vector<int> stt;
int timestamp;
void dfs(int u, int pre) {
stt[u] = timestamp++;
for (auto v : g[u]) {
if (v == pre) continue;
dfs(v, u);
}
}
};
#line 2 "graph/graph-template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <typename T = int>
struct Edge {
int from, to;
T cost;
int idx;
Edge() = default;
Edge(int from, int to, T cost = 1, int idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
operator int() const { return to; }
};
template <typename T = int>
struct Graph {
vector<vector<Edge<T> > > g;
int es;
Graph() = default;
explicit Graph(int n) : g(n), es(0) {}
size_t size() const { return g.size(); }
virtual void add_directed_edge(int from, int to, T cost = 1) {
g[from].emplace_back(from, to, cost, es++);
}
// virtual 可以被重载,实现多态
virtual void add_edge(int from, int to, T cost = 1) {
g[from].emplace_back(from, to, cost, es);
g[to].emplace_back(to, from, cost, es++);
}
void read(int M, int padding = -1, bool weighted = false,
bool directed = false) {
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a += padding;
b += padding;
T c = T(1);
if (weighted) cin >> c;
if (directed)
add_directed_edge(a, b, c);
else
add_edge(a, b, c);
}
}
inline vector<Edge<T> > &operator[](const int &k) { return g[k]; }
inline const vector<Edge<T> > &operator[](const int &k) const { return g[k]; }
};
template <typename T = int>
using Edges = vector<Edge<T> >;
#line 3 "graph/tree/doubling-lowest-common-ancestor.hpp"
/*
树上倍增找 lca
*/
template <typename T = int>
struct DoublingLowestCommonAncestor {
explicit DoublingLowestCommonAncestor() {}
explicit DoublingLowestCommonAncestor(const Graph<T> &g, int root = 0) : g(g) {
build(root);
}
void build(int root = 0) {
LOG = 32 - __builtin_clz(g.size());
dep.assign(g.size(), 0);
sum.assign(g.size(), 0);
table.assign(LOG, vector<int>(g.size(), -1));
dfs(root, -1, 0);
for (int k = 0; k + 1 < LOG; k++) {
for (int i = 0; i < (int)table[k].size(); i++) {
if (table[k][i] == -1)
table[k + 1][i] = -1;
else
table[k + 1][i] = table[k][table[k][i]];
}
}
}
int lca(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
v = climb(v, dep[v] - dep[u]);
if (u == v) return u;
for (int i = LOG - 1; i >= 0; i--) {
if (table[i][u] != table[i][v]) {
u = table[i][u];
v = table[i][v];
}
}
return table[0][u];
}
int climb(int u, int k) {
if (dep[u] < k) return -1;
for (int i = LOG - 1; i >= 0; i--) {
if ((k >> i) & 1) u = table[i][u];
}
return u;
}
T dist(int u, int v) { return sum[u] + sum[v] - 2 * sum[lca(u, v)]; }
private:
vector<int> dep;
vector<T> sum;
vector<vector<int> > table;
int LOG;
const Graph<T> &g;
void dfs(int idx, int par, int d) {
table[0][idx] = par;
dep[idx] = d;
for (auto &to : g[idx]) {
if (to != par) {
sum[to] = sum[idx] + to.cost;
dfs(to, idx, d + 1);
}
}
}
};
#line 3 "graph/tree/virtual-tree.hpp"
template <typename T>
struct VirtualTree {
public:
VirtualTree() {}
Graph<T> build(const Graph<T>& g, const vector<int>& points, int root = 0) {
DoublingLowestCommonAncestor<T> LCA = DoublingLowestCommonAncestor(g, root);
timestamp = 0;
int n = g.size();
stt.resize(n);
dfs(root, -1);
auto p = points;
sort(p.begin(), p.end(), [&](int x, int y) {
return stt[x] < stt[y];
});
int sz = points.size();
for (int i = 0; i < sz - 1; i++) {
p.push_back(LCA.lca(p[i], p[i + 1]));
}
sort(p.begin(), p.end(), [&](int x, int y) {
return stt[x] < stt[y];
});
p.resize(unique(p.begin(), p.end()) - p.begin());
sz = p.size();
Graph<T> g2(n);
for (int i = 0; i < sz - 1; i++) {
int lc = LCA.lca(p[i], p[i + 1]);
g2.add_edge(lc, p[i + 1]);
}
return g2;
}
private:
vector<int> stt;
int timestamp;
void dfs(int u, int pre) {
stt[u] = timestamp++;
for (auto v : g[u]) {
if (v == pre) continue;
dfs(v, u);
}
}
};