#pragma once
#include <cassert>
#include <utility>
#include <vector>
using namespace std;
#include "convert-tree.hpp"
#include "heavy-light-decomposition.hpp"
namespace StaticTopTreeImpl {
enum Type { Edge, Compress, Rake };
template <typename G>
struct StaticTopTree {
const HeavyLightDecomposition<G>& hld;
vector<vector<int>> g;
int root; // 元の木の root
int tt_root; // top tree の root
vector<int> P, L, R;
vector<Type> T;
StaticTopTree(const HeavyLightDecomposition<G>& _hld) : hld(_hld) {
root = hld.root;
g = rooted_tree(hld.g, root);
int n = g.size();
P.resize(n, -1), L.resize(n, -1), R.resize(n, -1);
T.resize(n, Type::Edge);
build();
}
private:
int add(int l, int r, Type t) {
if (t == Type::Compress or t == Type::Rake) {
assert(l != -1 and r != -1);
}
assert(t != Type::Edge);
int k = P.size();
P.push_back(-1), L.push_back(l), R.push_back(r), T.push_back(t);
if (l != -1) P[l] = k;
if (r != -1) P[r] = k;
return k;
}
pair<int, int> merge(const vector<pair<int, int>>& a, Type t) {
assert(!a.empty());
if (a.size() == 1) return a[0];
int sum_s = 0;
for (auto& [_, s] : a) sum_s += s;
vector<pair<int, int>> b, c;
for (auto& [i, s] : a) {
(sum_s > s ? b : c).emplace_back(i, s);
sum_s -= s * 2;
}
auto [i, si] = merge(b, t);
auto [j, sj] = merge(c, t);
return {add(i, j, t), si + sj};
}
pair<int, int> compress(int i) {
vector<pair<int, int>> chs{{i, 1}};
while (!g[i].empty()) {
chs.push_back(rake(i));
i = g[i][0];
}
return merge(chs, Type::Compress);
}
pair<int, int> rake(int i) {
vector<pair<int, int>> chs{{g[i][0], 1}};
for (int j = 1; j < (int)g[i].size(); j++) chs.push_back(compress(g[i][j]));
return merge(chs, Type::Rake);
}
void build() {
auto [i, n] = compress(root);
assert((int)g.size() == n);
assert((int)P.size() == n * 2 - 1);
tt_root = i;
}
};
template <typename G, typename Data, typename Edge, typename Compress,
typename Rake>
struct DPonStaticTopTree {
const StaticTopTree<G> tt;
vector<Data> dat;
const Edge edge;
const Compress compress;
const Rake rake;
DPonStaticTopTree(const HeavyLightDecomposition<G>& hld, const Edge& _edge,
const Compress& _compress, const Rake& _rake)
: tt(hld), edge(_edge), compress(_compress), rake(_rake) {
int n = tt.P.size();
dat.resize(n);
dfs(tt.tt_root);
}
Data get() { return dat[tt.tt_root]; }
void update(int k) {
while (k != -1) _update(k), k = tt.P[k];
}
private:
void _update(int k) {
if (tt.T[k] == Type::Edge) {
dat[k] = edge(k);
} else if (tt.T[k] == Type::Compress) {
dat[k] = compress(dat[tt.L[k]], dat[tt.R[k]]);
} else if (tt.T[k] == Type::Rake) {
dat[k] = rake(dat[tt.L[k]], dat[tt.R[k]]);
}
}
void dfs(int k) {
if (tt.L[k] != -1) dfs(tt.L[k]);
if (tt.R[k] != -1) dfs(tt.R[k]);
_update(k);
}
};
} // namespace StaticTopTreeImpl
using StaticTopTreeImpl::DPonStaticTopTree;
using StaticTopTreeImpl::StaticTopTree;
/*
// template
using Data = ;
auto edge = [&](int i) -> Data {
};
auto compress = [&](const Data& p, const Data& c) -> Data {
};
auto rake = [&](const Data& p, const Data& c) -> Data {
};
HeavyLightDecomposition hld{g};
DPonStaticTopTree<vector<vector<int>>, Data, decltype(edge),
decltype(compress), decltype(rake)>
dp(hld, edge, compress, rake);
*/
#line 2 "tree/static-top-tree-edge-based.hpp"
#include <cassert>
#include <utility>
#include <vector>
using namespace std;
#line 2 "tree/convert-tree.hpp"
#line 2 "graph/graph-template.hpp"
template <typename T>
struct edge {
int src, to;
T cost;
edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;
// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
UnweightedGraph g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
if (is_1origin) x--, y--;
g[x].push_back(y);
if (!is_directed) g[y].push_back(x);
}
return g;
}
// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
WeightedGraph<T> g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
cin >> c;
if (is_1origin) x--, y--;
g[x].emplace_back(x, y, c);
if (!is_directed) g[y].emplace_back(y, x, c);
}
return g;
}
// Input of Edges
template <typename T>
Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true,
bool is_1origin = true) {
Edges<T> es;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
es.emplace_back(x, y, c);
}
return es;
}
// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
bool is_directed = false, bool is_1origin = true) {
vector<vector<T>> d(N, vector<T>(N, INF));
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
d[x][y] = c;
if (!is_directed) d[y][x] = c;
}
return d;
}
/**
* @brief グラフテンプレート
* @docs docs/graph/graph-template.md
*/
#line 4 "tree/convert-tree.hpp"
template <typename T>
struct has_cost {
private:
template <typename U>
static auto confirm(U u) -> decltype(u.cost, std::true_type());
static auto confirm(...) -> std::false_type;
public:
enum : bool { value = decltype(confirm(std::declval<T>()))::value };
};
template <typename T>
vector<vector<T>> inverse_tree(const vector<vector<T>>& g) {
int N = (int)g.size();
vector<vector<T>> rg(N);
for (int i = 0; i < N; i++) {
for (auto& e : g[i]) {
if constexpr (is_same<T, int>::value) {
rg[e].push_back(i);
} else if constexpr (has_cost<T>::value) {
rg[e].emplace_back(e.to, i, e.cost);
} else {
assert(0);
}
}
}
return rg;
}
template <typename T>
vector<vector<T>> rooted_tree(const vector<vector<T>>& g, int root = 0) {
int N = (int)g.size();
vector<vector<T>> rg(N);
vector<char> v(N, false);
v[root] = true;
queue<int> que;
que.emplace(root);
while (!que.empty()) {
auto p = que.front();
que.pop();
for (auto& e : g[p]) {
if (v[e] == false) {
v[e] = true;
que.push(e);
rg[p].push_back(e);
}
}
}
return rg;
}
/**
* @brief 根付き木・逆辺からなる木への変換
*/
#line 2 "tree/heavy-light-decomposition.hpp"
#line 4 "tree/heavy-light-decomposition.hpp"
template <typename G>
struct HeavyLightDecomposition {
private:
void dfs_sz(int cur) {
size[cur] = 1;
for (auto& dst : g[cur]) {
if (dst == par[cur]) {
if (g[cur].size() >= 2 && int(dst) == int(g[cur][0]))
swap(g[cur][0], g[cur][1]);
else
continue;
}
depth[dst] = depth[cur] + 1;
par[dst] = cur;
dfs_sz(dst);
size[cur] += size[dst];
if (size[dst] > size[g[cur][0]]) {
swap(dst, g[cur][0]);
}
}
}
void dfs_hld(int cur) {
down[cur] = id++;
for (auto dst : g[cur]) {
if (dst == par[cur]) continue;
nxt[dst] = (int(dst) == int(g[cur][0]) ? nxt[cur] : int(dst));
dfs_hld(dst);
}
up[cur] = id;
}
// [u, v)
vector<pair<int, int>> ascend(int u, int v) const {
vector<pair<int, int>> res;
while (nxt[u] != nxt[v]) {
res.emplace_back(down[u], down[nxt[u]]);
u = par[nxt[u]];
}
if (u != v) res.emplace_back(down[u], down[v] + 1);
return res;
}
// (u, v]
vector<pair<int, int>> descend(int u, int v) const {
if (u == v) return {};
if (nxt[u] == nxt[v]) return {{down[u] + 1, down[v]}};
auto res = descend(u, par[nxt[v]]);
res.emplace_back(down[nxt[v]], down[v]);
return res;
}
public:
G& g;
int root, id;
vector<int> size, depth, down, up, nxt, par;
HeavyLightDecomposition(G& _g, int _root = 0)
: g(_g),
root(_root),
id(0),
size(g.size(), 0),
depth(g.size(), 0),
down(g.size(), -1),
up(g.size(), -1),
nxt(g.size(), root),
par(g.size(), root) {
dfs_sz(root);
dfs_hld(root);
}
pair<int, int> idx(int i) const { return make_pair(down[i], up[i]); }
template <typename F>
void path_query(int u, int v, bool vertex, const F& f) {
int l = lca(u, v);
for (auto&& [a, b] : ascend(u, l)) {
int s = a + 1, t = b;
s > t ? f(t, s) : f(s, t);
}
if (vertex) f(down[l], down[l] + 1);
for (auto&& [a, b] : descend(l, v)) {
int s = a, t = b + 1;
s > t ? f(t, s) : f(s, t);
}
}
template <typename F>
void path_noncommutative_query(int u, int v, bool vertex, const F& f) {
int l = lca(u, v);
for (auto&& [a, b] : ascend(u, l)) f(a + 1, b);
if (vertex) f(down[l], down[l] + 1);
for (auto&& [a, b] : descend(l, v)) f(a, b + 1);
}
template <typename F>
void subtree_query(int u, bool vertex, const F& f) {
f(down[u] + int(!vertex), up[u]);
}
int lca(int a, int b) {
while (nxt[a] != nxt[b]) {
if (down[a] < down[b]) swap(a, b);
a = par[nxt[a]];
}
return depth[a] < depth[b] ? a : b;
}
int dist(int a, int b) { return depth[a] + depth[b] - depth[lca(a, b)] * 2; }
};
/**
* @brief Heavy Light Decomposition(重軽分解)
* @docs docs/tree/heavy-light-decomposition.md
*/
#line 10 "tree/static-top-tree-edge-based.hpp"
namespace StaticTopTreeImpl {
enum Type { Edge, Compress, Rake };
template <typename G>
struct StaticTopTree {
const HeavyLightDecomposition<G>& hld;
vector<vector<int>> g;
int root; // 元の木の root
int tt_root; // top tree の root
vector<int> P, L, R;
vector<Type> T;
StaticTopTree(const HeavyLightDecomposition<G>& _hld) : hld(_hld) {
root = hld.root;
g = rooted_tree(hld.g, root);
int n = g.size();
P.resize(n, -1), L.resize(n, -1), R.resize(n, -1);
T.resize(n, Type::Edge);
build();
}
private:
int add(int l, int r, Type t) {
if (t == Type::Compress or t == Type::Rake) {
assert(l != -1 and r != -1);
}
assert(t != Type::Edge);
int k = P.size();
P.push_back(-1), L.push_back(l), R.push_back(r), T.push_back(t);
if (l != -1) P[l] = k;
if (r != -1) P[r] = k;
return k;
}
pair<int, int> merge(const vector<pair<int, int>>& a, Type t) {
assert(!a.empty());
if (a.size() == 1) return a[0];
int sum_s = 0;
for (auto& [_, s] : a) sum_s += s;
vector<pair<int, int>> b, c;
for (auto& [i, s] : a) {
(sum_s > s ? b : c).emplace_back(i, s);
sum_s -= s * 2;
}
auto [i, si] = merge(b, t);
auto [j, sj] = merge(c, t);
return {add(i, j, t), si + sj};
}
pair<int, int> compress(int i) {
vector<pair<int, int>> chs{{i, 1}};
while (!g[i].empty()) {
chs.push_back(rake(i));
i = g[i][0];
}
return merge(chs, Type::Compress);
}
pair<int, int> rake(int i) {
vector<pair<int, int>> chs{{g[i][0], 1}};
for (int j = 1; j < (int)g[i].size(); j++) chs.push_back(compress(g[i][j]));
return merge(chs, Type::Rake);
}
void build() {
auto [i, n] = compress(root);
assert((int)g.size() == n);
assert((int)P.size() == n * 2 - 1);
tt_root = i;
}
};
template <typename G, typename Data, typename Edge, typename Compress,
typename Rake>
struct DPonStaticTopTree {
const StaticTopTree<G> tt;
vector<Data> dat;
const Edge edge;
const Compress compress;
const Rake rake;
DPonStaticTopTree(const HeavyLightDecomposition<G>& hld, const Edge& _edge,
const Compress& _compress, const Rake& _rake)
: tt(hld), edge(_edge), compress(_compress), rake(_rake) {
int n = tt.P.size();
dat.resize(n);
dfs(tt.tt_root);
}
Data get() { return dat[tt.tt_root]; }
void update(int k) {
while (k != -1) _update(k), k = tt.P[k];
}
private:
void _update(int k) {
if (tt.T[k] == Type::Edge) {
dat[k] = edge(k);
} else if (tt.T[k] == Type::Compress) {
dat[k] = compress(dat[tt.L[k]], dat[tt.R[k]]);
} else if (tt.T[k] == Type::Rake) {
dat[k] = rake(dat[tt.L[k]], dat[tt.R[k]]);
}
}
void dfs(int k) {
if (tt.L[k] != -1) dfs(tt.L[k]);
if (tt.R[k] != -1) dfs(tt.R[k]);
_update(k);
}
};
} // namespace StaticTopTreeImpl
using StaticTopTreeImpl::DPonStaticTopTree;
using StaticTopTreeImpl::StaticTopTree;
/*
// template
using Data = ;
auto edge = [&](int i) -> Data {
};
auto compress = [&](const Data& p, const Data& c) -> Data {
};
auto rake = [&](const Data& p, const Data& c) -> Data {
};
HeavyLightDecomposition hld{g};
DPonStaticTopTree<vector<vector<int>>, Data, decltype(edge),
decltype(compress), decltype(rake)>
dp(hld, edge, compress, rake);
*/