This documentation is automatically generated by online-judge-tools/verification-helper
#include "graph/heavy_light_decomposition.hpp"
#pragma once
#include <algorithm>
#include <utility>
#include "graph.hpp"
class HeavyLightDecomposition {
public:
std::vector<int> siz, par, hea, in, out, dep, rev;
private:
template <typename T, bool DIR>
void dfs1(Graph<T, DIR> &g, int v) {
if (g[v].size() >= 1 && g[v][0].to == par[v]) {
std::swap(g[v][0], g[v][g[v].size() - 1]);
}
for (Edge<T> &e : g[v]) {
if (e.to != par[v]) {
par[e.to] = v;
dfs1(g, e.to);
siz[v] += siz[e.to];
if (siz[e.to] > siz[g[v][0].to]) {
std::swap(g[v][0], e);
}
}
}
}
template <typename T, bool DIR>
void dfs2(const Graph<T, DIR> &g, int v, int &t) {
in[v] = t;
rev[t++] = v;
for (const Edge<T> &e : g[v]) {
if (e.to == par[v]) {
continue;
}
if (e.to == g[v][0].to) {
hea[e.to] = hea[v];
} else {
hea[e.to] = e.to;
}
dep[e.to] = dep[v] + 1;
dfs2(g, e.to, t);
}
out[v] = t;
}
public:
template <typename T, bool DIR>
HeavyLightDecomposition(Graph<T, DIR> &g, int root = 0)
: siz(g.v(), 1),
par(g.v(), root),
hea(g.v(), root),
in(g.v(), 0),
out(g.v(), 0),
dep(g.v(), 0),
rev(g.v(), 0) {
assert(0 <= root && root < g.v());
dfs1(g, root);
int t = 0;
dfs2(g, root, t);
}
// par^k
int la(int v, int k) const {
assert(0 <= v && v < (int)dep.size());
assert(k >= 0);
if (k > dep[v]) {
return -1;
}
while (true) {
int u = hea[v];
if (in[u] + k <= in[v]) {
return rev[in[v] - k];
}
k -= in[v] - in[u] + 1;
v = par[u];
}
return 0;
}
int lca(int u, int v) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
while (u != v) {
if (in[u] > in[v]) {
std::swap(u, v);
}
if (hea[u] == hea[v]) {
v = u;
} else {
v = par[hea[v]];
}
}
return u;
}
int dist(int u, int v) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
int jump(int u, int v, int k) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
assert(k >= 0);
int l = lca(u, v);
int dis = dep[u] + dep[v] - 2 * dep[l];
if (k > dis) {
return -1;
}
if (k <= dep[u] - dep[l]) {
return la(u, k);
} else {
return la(v, dis - k);
}
}
int meet(int u, int v, int w) const {
return lca(u, v) ^ lca(v, w) ^ lca(w, u);
}
std::vector<std::pair<int, int>> path(int u, int v, bool edge) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
std::vector<std::pair<int, int>> fromu, fromv;
bool rev = false;
while (true) {
if (u == v && edge) {
break;
}
if (in[u] > in[v]) {
std::swap(u, v);
std::swap(fromu, fromv);
rev ^= true;
}
if (hea[u] == hea[v]) {
fromv.emplace_back(in[v], in[u] + (int)edge);
v = u;
break;
} else {
fromv.emplace_back(in[v], in[hea[v]]);
v = par[hea[v]];
}
}
if (rev) {
std::swap(fromu, fromv);
}
std::reverse(fromv.begin(), fromv.end());
fromu.reserve(fromv.size());
for (auto [x, y] : fromv) {
fromu.emplace_back(y, x);
}
return fromu;
}
};
#line 2 "graph/heavy_light_decomposition.hpp"
#include <algorithm>
#include <utility>
#line 2 "graph/graph.hpp"
#include <iostream>
#include <cassert>
#include <vector>
template <typename T>
struct Edge {
using W = T;
int from, to, id;
W weight;
Edge<T> rev() const {
return Edge<T>{to, from, id, weight};
}
};
template <typename T>
void debug(const Edge<T> &e) {
std::cerr << e.from << " -> " << e.to << " id = " << e.id << std::cerr << " weight = ";
debug(e.weight);
}
template <typename T = int, bool DIR = false>
class Graph {
public:
using E = Edge<T>;
using W = T;
static constexpr bool DIRECTED = DIR;
struct Adjacency {
using Iter = typename std::vector<E>::iterator;
Iter be, en;
Iter begin() const { return be; }
Iter end() const { return en; }
int size() const { return (int)std::distance(be, en); }
E &operator[](int idx) const { return be[idx]; }
};
struct ConstAdjacency {
using Iter = typename std::vector<E>::const_iterator;
Iter be, en;
Iter begin() const { return be; }
Iter end() const { return en; }
int size() const { return (int)std::distance(be, en); }
const E &operator[](int idx) const { return be[idx]; }
};
private:
int n, m;
std::vector<E> edges, csr;
std::vector<int> sep;
bool built;
public:
Graph(int n) : n(n), m(0), built(false) {}
int v() const { return n; }
int e() const { return m; }
int add_vertex() {
return n++;
}
void add_edge(int from, int to, W weight = 1) {
assert(0 <= from && from < n && 0 <= to && to < n);
edges.emplace_back(E{from, to, m++, weight});
}
void build() {
sep.assign(n + 1, 0);
csr.resize(DIRECTED ? m : 2 * m);
for (const E &e : edges) {
++sep[e.from + 1];
if (!DIRECTED) {
++sep[e.to + 1];
}
}
for (int i = 0; i < n; ++i) {
sep[i + 1] += sep[i];
}
std::vector<int> c = sep;
for (const E &e : edges) {
csr[c[e.from]++] = e;
if (!DIRECTED) {
csr[c[e.to]++] = e.rev();
}
}
built = true;
}
Adjacency operator[](int v) {
assert(built && 0 <= v && v < n);
return Adjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]};
}
ConstAdjacency operator[](int v) const {
assert(built && 0 <= v && v < n);
return ConstAdjacency{csr.begin() + sep[v], csr.begin() + sep[v + 1]};
}
};
#line 5 "graph/heavy_light_decomposition.hpp"
class HeavyLightDecomposition {
public:
std::vector<int> siz, par, hea, in, out, dep, rev;
private:
template <typename T, bool DIR>
void dfs1(Graph<T, DIR> &g, int v) {
if (g[v].size() >= 1 && g[v][0].to == par[v]) {
std::swap(g[v][0], g[v][g[v].size() - 1]);
}
for (Edge<T> &e : g[v]) {
if (e.to != par[v]) {
par[e.to] = v;
dfs1(g, e.to);
siz[v] += siz[e.to];
if (siz[e.to] > siz[g[v][0].to]) {
std::swap(g[v][0], e);
}
}
}
}
template <typename T, bool DIR>
void dfs2(const Graph<T, DIR> &g, int v, int &t) {
in[v] = t;
rev[t++] = v;
for (const Edge<T> &e : g[v]) {
if (e.to == par[v]) {
continue;
}
if (e.to == g[v][0].to) {
hea[e.to] = hea[v];
} else {
hea[e.to] = e.to;
}
dep[e.to] = dep[v] + 1;
dfs2(g, e.to, t);
}
out[v] = t;
}
public:
template <typename T, bool DIR>
HeavyLightDecomposition(Graph<T, DIR> &g, int root = 0)
: siz(g.v(), 1),
par(g.v(), root),
hea(g.v(), root),
in(g.v(), 0),
out(g.v(), 0),
dep(g.v(), 0),
rev(g.v(), 0) {
assert(0 <= root && root < g.v());
dfs1(g, root);
int t = 0;
dfs2(g, root, t);
}
// par^k
int la(int v, int k) const {
assert(0 <= v && v < (int)dep.size());
assert(k >= 0);
if (k > dep[v]) {
return -1;
}
while (true) {
int u = hea[v];
if (in[u] + k <= in[v]) {
return rev[in[v] - k];
}
k -= in[v] - in[u] + 1;
v = par[u];
}
return 0;
}
int lca(int u, int v) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
while (u != v) {
if (in[u] > in[v]) {
std::swap(u, v);
}
if (hea[u] == hea[v]) {
v = u;
} else {
v = par[hea[v]];
}
}
return u;
}
int dist(int u, int v) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
int jump(int u, int v, int k) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
assert(k >= 0);
int l = lca(u, v);
int dis = dep[u] + dep[v] - 2 * dep[l];
if (k > dis) {
return -1;
}
if (k <= dep[u] - dep[l]) {
return la(u, k);
} else {
return la(v, dis - k);
}
}
int meet(int u, int v, int w) const {
return lca(u, v) ^ lca(v, w) ^ lca(w, u);
}
std::vector<std::pair<int, int>> path(int u, int v, bool edge) const {
assert(0 <= u && u < (int)dep.size());
assert(0 <= v && v < (int)dep.size());
std::vector<std::pair<int, int>> fromu, fromv;
bool rev = false;
while (true) {
if (u == v && edge) {
break;
}
if (in[u] > in[v]) {
std::swap(u, v);
std::swap(fromu, fromv);
rev ^= true;
}
if (hea[u] == hea[v]) {
fromv.emplace_back(in[v], in[u] + (int)edge);
v = u;
break;
} else {
fromv.emplace_back(in[v], in[hea[v]]);
v = par[hea[v]];
}
}
if (rev) {
std::swap(fromu, fromv);
}
std::reverse(fromv.begin(), fromv.end());
fromu.reserve(fromv.size());
for (auto [x, y] : fromv) {
fromu.emplace_back(y, x);
}
return fromu;
}
};