#pragma once #include <vector> #include "Mylib/Graph/Template/graph.cpp" namespace haar_lib { template <typename T> class centroid_decomposition { int N_; std::vector<int> parent_, subsize_; std::vector<std::vector<int>> children_; std::vector<bool> check_; public: centroid_decomposition() {} centroid_decomposition(const tree<T> &tr) : N_(tr.size()), parent_(N_), subsize_(N_), children_(N_), check_(N_) { decompose(tr, 0, -1); } private: void decompose(const tree<T> &tr, int cur, int par) { dfs_subsize(tr, cur, -1); auto c = get_centroid(tr, cur, -1, subsize_[cur]); check_[c] = true; parent_[c] = par; if (par != -1) children_[par].push_back(c); for (auto &e : tr[c]) { if (check_[e.to]) continue; decompose(tr, e.to, c); } } int get_centroid(const tree<T> &tr, int cur, int par, int total_size) { for (auto &e : tr[cur]) { if (e.to == par or check_[e.to]) continue; if (2 * subsize_[e.to] > total_size) { return get_centroid(tr, e.to, cur, total_size); } } return cur; } void dfs_subsize(const tree<T> &tr, int cur, int par) { subsize_[cur] = 1; for (auto &e : tr[cur]) { if (e.to == par or check_[e.to]) continue; dfs_subsize(tr, e.to, cur); subsize_[cur] += subsize_[e.to]; } } public: auto bottom_up(int i) const { std::vector<int> ret; while (i >= 0) { ret.push_back(i); i = parent_[i]; } return ret; } }; } // namespace haar_lib
#line 2 "Mylib/Graph/TreeUtils/centroid_decomposition.cpp" #include <vector> #line 2 "Mylib/Graph/Template/graph.cpp" #include <iostream> #line 4 "Mylib/Graph/Template/graph.cpp" namespace haar_lib { template <typename T> struct edge { int from, to; T cost; int index = -1; edge() {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int index) : from(from), to(to), cost(cost), index(index) {} }; template <typename T> struct graph { using weight_type = T; using edge_type = edge<T>; std::vector<std::vector<edge<T>>> data; auto& operator[](size_t i) { return data[i]; } const auto& operator[](size_t i) const { return data[i]; } auto begin() const { return data.begin(); } auto end() const { return data.end(); } graph() {} graph(int N) : data(N) {} bool empty() const { return data.empty(); } int size() const { return data.size(); } void add_edge(int i, int j, T w, int index = -1) { data[i].emplace_back(i, j, w, index); } void add_undirected(int i, int j, T w, int index = -1) { add_edge(i, j, w, index); add_edge(j, i, w, index); } template <size_t I, bool DIRECTED = true, bool WEIGHTED = true> void read(int M) { for (int i = 0; i < M; ++i) { int u, v; std::cin >> u >> v; u -= I; v -= I; T w = 1; if (WEIGHTED) std::cin >> w; if (DIRECTED) add_edge(u, v, w, i); else add_undirected(u, v, w, i); } } }; template <typename T> using tree = graph<T>; } // namespace haar_lib #line 4 "Mylib/Graph/TreeUtils/centroid_decomposition.cpp" namespace haar_lib { template <typename T> class centroid_decomposition { int N_; std::vector<int> parent_, subsize_; std::vector<std::vector<int>> children_; std::vector<bool> check_; public: centroid_decomposition() {} centroid_decomposition(const tree<T> &tr) : N_(tr.size()), parent_(N_), subsize_(N_), children_(N_), check_(N_) { decompose(tr, 0, -1); } private: void decompose(const tree<T> &tr, int cur, int par) { dfs_subsize(tr, cur, -1); auto c = get_centroid(tr, cur, -1, subsize_[cur]); check_[c] = true; parent_[c] = par; if (par != -1) children_[par].push_back(c); for (auto &e : tr[c]) { if (check_[e.to]) continue; decompose(tr, e.to, c); } } int get_centroid(const tree<T> &tr, int cur, int par, int total_size) { for (auto &e : tr[cur]) { if (e.to == par or check_[e.to]) continue; if (2 * subsize_[e.to] > total_size) { return get_centroid(tr, e.to, cur, total_size); } } return cur; } void dfs_subsize(const tree<T> &tr, int cur, int par) { subsize_[cur] = 1; for (auto &e : tr[cur]) { if (e.to == par or check_[e.to]) continue; dfs_subsize(tr, e.to, cur); subsize_[cur] += subsize_[e.to]; } } public: auto bottom_up(int i) const { std::vector<int> ret; while (i >= 0) { ret.push_back(i); i = parent_[i]; } return ret; } }; } // namespace haar_lib