This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "graph/tree/centroids.hpp"
#pragma once
#include "graph.hpp"
#include "prelude.hpp"
struct cd_result {
public:
int root, max_depth;
vector<int> par, ord, depth;
vector<vector<int>> ch;
// desc[c] = tree with centroid c, sorted by dist
vector<vector<int>> desc;
// dist[d][v] = distance to centroid with depth d
vector<vector<int>> dist;
int dist_from_centroid(int c, int v) const { return dist[depth[c]][v]; }
using weight_type = void;
template <class F>
void adj(int v, F f) const {
if (par[v] != -1) f(unit_edge{par[v]});
for (auto u : ch[v]) f(unit_edge{u});
}
int deg(int v) const { return ch[v].size() + (par[v] != -1); }
int size() const { return par.size(); }
};
// Assumes connectedness
template <class G, class = decltype(declval<G>()[int{}])>
cd_result centroid_decomposition(const G& graph) {
graph_trait g(graph);
int n = g.size();
vector<int> par(n), ord, depth(n, -1), sz(n);
vector<vector<int>> ch(n), desc(n), dist;
vector<int> cnt(n);
auto main = [&](auto&& f_main, int r, int d) -> int {
auto sz_dfs = [&](auto&& f, int v, int p) -> int {
int sz_v = 1;
for (int u : g[v])
if (depth[u] == -1 && u != p) sz_v += f(f, u, v);
return sz[v] = sz_v;
};
sz_dfs(sz_dfs, r, -1);
int total = sz[r];
auto find_c = [&](auto&& f, int v, int p) -> int {
for (int u : g[v])
if (depth[u] == -1 && u != p && sz[u] * 2 > total) return f(f, u, v);
return v;
};
int c = find_c(find_c, r, -1);
ord.push_back(c);
depth[c] = d;
if (dist.size() <= d) dist.resize(d + 1);
dist[d].resize(n, INT_MAX);
auto& desc_c = desc[c];
auto& dists_d = dist[d];
desc_c.reserve(total);
int max_d = 0;
auto dist_dfs = [&](auto&& f, int v, int p, int dis) -> void {
desc_c.push_back(v);
dists_d[v] = dis;
max_d = max(max_d, dis);
for (int u : g[v])
if (depth[u] == -1 && u != p) f(f, u, v, dis + 1);
};
dist_dfs(dist_dfs, c, -1, 0);
fill(cnt.begin(), cnt.begin() + max_d + 1, 0);
for (auto v : desc_c) cnt[dists_d[v]]++;
partial_sum(cnt.begin(), cnt.begin() + max_d + 1, cnt.begin());
vector<int> desc2(total);
for (auto v : desc_c) desc2[--cnt[dists_d[v]]] = v;
desc_c = move(desc2);
for (auto u : g[c])
if (depth[u] == -1) {
int c2 = f_main(f_main, u, d + 1);
par[c2] = c;
ch[c].push_back(c2);
}
return c;
};
int c = main(main, 0, 0);
par[c] = -1;
int max_depth = *max_element(all(depth));
return {c, max_depth, par, ord, depth, ch, desc, dist};
}
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "graph/traits.hpp"
struct unit_edge {
int to;
operator int() const { return to; }
int w() const { return 1; }
};
template <class Weight>
struct weighted_edge {
int to;
Weight weight;
operator int() const { return to; }
Weight w() const { return weight; }
};
template <class Inner>
struct basic_graph {
using weight_type = void;
const Inner& inner;
basic_graph(const Inner& g) : inner(g) { }
template <class F>
void adj(int v, F f) const {
for (auto u : inner[v]) f(unit_edge{u});
}
int deg(int v) const { return inner[v].size(); }
};
template <class Inner, class Weight>
struct basic_weighted_graph {
using weight_type = Weight;
const Inner& inner;
basic_weighted_graph(const Inner& g) : inner(g) { }
template <class F>
void adj(int v, F f) const {
for (auto [u, w] : inner[v]) f(weighted_edge<weight_type>{u, w});
}
int deg(int v) const { return inner[v].size(); }
};
template <class Inner>
struct graph_trait {
using weight_type = typename Inner::weight_type;
const Inner& g;
graph_trait(const Inner& g) : g(g) { }
int size() const { return g.size(); }
template <class F>
void adj(int v, F f) const {
g.adj(v, f);
}
decltype(auto) operator[](int v) const { return g[v]; }
};
template <class T>
constexpr bool is_weighted_v =
!is_same_v<typename graph_trait<T>::weight_type, void>;
template <class T>
using weight_t =
conditional_t<is_weighted_v<T>, typename graph_trait<T>::weight_type, int>;
template <class T>
using edge_t =
conditional_t<is_weighted_v<T>, weighted_edge<weight_t<T>>, unit_edge>;
template <size_t N>
struct graph_trait<vector<int>[N]> : basic_graph<vector<int>[N]> {
using basic_graph<vector<int>[N]>::basic_graph;
int size() const { return N; }
};
template <>
struct graph_trait<vector<vector<int>>> : basic_graph<vector<vector<int>>> {
using basic_graph<vector<vector<int>>>::basic_graph;
int size() const { return this->inner.size(); }
};
template <size_t N, class Weight>
struct graph_trait<vector<pair<int, Weight>>[N]>
: basic_weighted_graph<vector<pair<int, Weight>>[N], Weight> {
using basic_weighted_graph<
vector<pair<int, Weight>>[N], Weight>::basic_weighted_graph;
int size() const { return N; }
};
template <class Weight>
struct graph_trait<vector<vector<pair<int, Weight>>>>
: basic_weighted_graph<vector<vector<pair<int, Weight>>>, Weight> {
using basic_weighted_graph<
vector<vector<pair<int, Weight>>>, Weight>::basic_weighted_graph;
int size() const { return this->inner.size(); }
};
#line 3 "range.hpp"
template <class It>
struct range : pair<It, It> {
using pair<It, It>::pair;
It begin() const { return this->first; }
It end() const { return this->second; }
It cbegin() const { return begin(); }
It cend() const { return end(); }
int size() const { return this->second - this->first; }
};
#line 4 "graph/csr.hpp"
template <size_t>
struct stdin_reader;
template <class Weight = void>
class csr_graph {
private:
struct directed_t {};
public:
using weight_type = Weight;
csr_graph() = default;
template <class It>
csr_graph(int n, It e, It e_last) : n(n), m(distance(e, e_last)) {
init<false>(e, e_last);
}
template <size_t Size = 1 << 26>
csr_graph(int n, int m, stdin_reader<Size>& read) : n(n), m(m) {
auto e = read_e(read);
init<false>(all(e));
}
template <class It>
static csr_graph directed(int n, It e, It e_last) {
return csr_graph(directed_t{}, n, e, e_last);
}
template <size_t Size = 1 << 26>
static csr_graph directed(int n, int m, stdin_reader<Size>& read) {
return csr_graph(directed_t{}, n, m, read);
}
template <size_t Size = 1 << 26>
static csr_graph tree(int n, stdin_reader<Size>& read) {
return csr_graph(n, n - 1, read);
}
template <size_t Size = 1 << 26>
static csr_graph tree(stdin_reader<Size>& read) {
int n = read;
return csr_graph(n, n - 1, read);
}
int size() const { return n; }
range<typename vector<edge_t<csr_graph>>::iterator> operator[](int v) const {
return {ls[v], rs[v]};
}
int deg(int v) { return rs[v] - ls[v]; }
template <class F>
void adj(int v, F f) const {
for_each(ls[v], rs[v], f);
}
private:
template <class It>
csr_graph(directed_t, int n, It e, It e_last) : n(n), m(distance(e, e_last)) {
init<true>(e, e_last);
}
template <size_t Size = 1 << 26>
csr_graph(directed_t, int n, int m, stdin_reader<Size>& read) : n(n), m(m) {
auto e = read_e(read);
init<true>(all(e));
}
vector<typename vector<edge_t<csr_graph>>::iterator> ls, rs;
int n, m;
vector<edge_t<csr_graph>> es;
template <bool OneBased = true, size_t Size = 1 << 26>
auto read_e(stdin_reader<Size>& read) {
using E = conditional_t<is_weighted_v<csr_graph>, tuple<int, int, Weight>,
pair<int, int>>;
vector<E> res(m);
for (auto& e : res) {
read(e);
if (OneBased) get<0>(e)--, get<1>(e)--;
}
return res;
}
template <bool Directed, class It>
void init(It e, It e_last) {
if (!Directed) m *= 2;
es.resize(m);
ls.resize(n), rs.resize(n);
vector<int> sz(n);
for (auto it = e; it != e_last; it++) {
int from = get<0>(*it), to = get<1>(*it);
sz[from]++;
if (!Directed) sz[to]++;
}
partial_sum(all(sz), sz.begin());
rep(v, n) ls[v] = rs[v] = es.begin() + sz[v];
for (auto it = e; it != e_last; it++) {
int from = get<0>(*it), to = get<1>(*it);
if constexpr (is_weighted_v<csr_graph>)
*--ls[from] = edge_t<csr_graph>{to, get<2>(*it)};
else
*--ls[from] = edge_t<csr_graph>{to};
if (!Directed) {
if constexpr (is_weighted_v<csr_graph>)
*--ls[to] = edge_t<csr_graph>{from, get<2>(*it)};
else
*--ls[to] = edge_t<csr_graph>{from};
}
}
}
};
#line 4 "graph/tree/centroids.hpp"
struct cd_result {
public:
int root, max_depth;
vector<int> par, ord, depth;
vector<vector<int>> ch;
// desc[c] = tree with centroid c, sorted by dist
vector<vector<int>> desc;
// dist[d][v] = distance to centroid with depth d
vector<vector<int>> dist;
int dist_from_centroid(int c, int v) const { return dist[depth[c]][v]; }
using weight_type = void;
template <class F>
void adj(int v, F f) const {
if (par[v] != -1) f(unit_edge{par[v]});
for (auto u : ch[v]) f(unit_edge{u});
}
int deg(int v) const { return ch[v].size() + (par[v] != -1); }
int size() const { return par.size(); }
};
// Assumes connectedness
template <class G, class = decltype(declval<G>()[int{}])>
cd_result centroid_decomposition(const G& graph) {
graph_trait g(graph);
int n = g.size();
vector<int> par(n), ord, depth(n, -1), sz(n);
vector<vector<int>> ch(n), desc(n), dist;
vector<int> cnt(n);
auto main = [&](auto&& f_main, int r, int d) -> int {
auto sz_dfs = [&](auto&& f, int v, int p) -> int {
int sz_v = 1;
for (int u : g[v])
if (depth[u] == -1 && u != p) sz_v += f(f, u, v);
return sz[v] = sz_v;
};
sz_dfs(sz_dfs, r, -1);
int total = sz[r];
auto find_c = [&](auto&& f, int v, int p) -> int {
for (int u : g[v])
if (depth[u] == -1 && u != p && sz[u] * 2 > total) return f(f, u, v);
return v;
};
int c = find_c(find_c, r, -1);
ord.push_back(c);
depth[c] = d;
if (dist.size() <= d) dist.resize(d + 1);
dist[d].resize(n, INT_MAX);
auto& desc_c = desc[c];
auto& dists_d = dist[d];
desc_c.reserve(total);
int max_d = 0;
auto dist_dfs = [&](auto&& f, int v, int p, int dis) -> void {
desc_c.push_back(v);
dists_d[v] = dis;
max_d = max(max_d, dis);
for (int u : g[v])
if (depth[u] == -1 && u != p) f(f, u, v, dis + 1);
};
dist_dfs(dist_dfs, c, -1, 0);
fill(cnt.begin(), cnt.begin() + max_d + 1, 0);
for (auto v : desc_c) cnt[dists_d[v]]++;
partial_sum(cnt.begin(), cnt.begin() + max_d + 1, cnt.begin());
vector<int> desc2(total);
for (auto v : desc_c) desc2[--cnt[dists_d[v]]] = v;
desc_c = move(desc2);
for (auto u : g[c])
if (depth[u] == -1) {
int c2 = f_main(f_main, u, d + 1);
par[c2] = c;
ch[c].push_back(c2);
}
return c;
};
int c = main(main, 0, 0);
par[c] = -1;
int max_depth = *max_element(all(depth));
return {c, max_depth, par, ord, depth, ch, desc, dist};
}