#include using namespace std; using ll = long long; #define ALL(obj) (obj).begin(),(obj).end() #define SPEED cin.tie(0);ios::sync_with_stdio(false); //Union Find Tree class UnionFindTree { public: vector parent; vector rank; UnionFindTree(int N) : parent(N), rank(N,0){ for (int i = 0; i < N; ++i) parent[i] = i; } int root(int n) { return (parent[n] == n ? n : parent[n] = root(parent[n])); } int same(int n, int m) { return root(n) == root(m); } void unite(int n, int m) { n = root(n); m = root(m); if (n == m) return; if(rank[n] class Tree { Operator Op; using typeDist = decltype(Op.unitDist); size_t num; size_t ord; public: vector>> edge; vector depth; vector order; vector dist; vector> parent; vector>> child; vector,Operator::bit>> ancestor; vector size; vector> descendant; Tree(const int num):num(num),edge(num),depth(num,-1),order(num),dist(num){} //O(1) anytime void makeEdge(const int& from, const int& to, const typeDist w = 1) { edge[from].push_back({to,w}); } //O(N) anytime void makeDepth(const int root) { depth[root] = 0; dist[root] = Op.unitDist; ord = 0; dfs1(root); order[ord++] = root; } //O(N) anytime void makeDepth(void) { ord = 0; for(size_t root = 0; root < num; ++root) { if(depth[root] != -1) continue; depth[root] = 0; dist[root] = Op.unitDist; dfs1(root); order[ord++] = root; } } //for makeDepth void dfs1(int curr, int prev = -1){ for(auto& e:edge[curr]){ int next = e.first; if(next==prev) continue; depth[next] = depth[curr] + 1; dist[next] = Op.funcDist(dist[curr],e.second); dfs1(next,curr); order[ord++] = next; } } //O(N) after makeDepth void makeParent(void) { parent.resize(num,make_pair(num,Op.unitDist)); for (size_t i = 0; i < num; ++i) for (auto& e : edge[i]) if (depth[i] > depth[e.first]) parent[i] = e; } //O(N) after makeDepth void makeChild(void) { child.resize(num); for (size_t i = 0; i < num; ++i) for (auto& e : edge[i]) if (depth[i] < depth[e.first]) child[i].push_back(e); } //O(NlogN) after makeDepth and makeParent void makeAncestor(void) { ancestor.resize(num); for (size_t i = 0; i < num; ++i) ancestor[i][0] = (parent[i].first!=num?parent[i]:make_pair(i,Op.unitLca)); for (size_t j = 1; j < Operator::bit; ++j) { for (size_t i = 0; i < num; ++i) { size_t k = ancestor[i][j - 1].first; ancestor[i][j] = Op.funcLca(ancestor[k][j - 1],ancestor[i][j - 1]); } } } //O(logN) after makeAncestor //return {lca,lca_dist} l and r must be connected pair lca(size_t l, size_t r) { if (depth[l] < depth[r]) swap(l, r); int diff = depth[l] - depth[r]; auto ancl = make_pair(l,Op.unitLca); auto ancr = make_pair(r,Op.unitLca); for (int j = 0; j < Operator::bit; ++j) { if (diff & (1 << j)) { ancl = Op.funcLca(ancestor[ancl.first][j],ancl); } } if(ancl.first==ancr.first) return ancl; for (int j = Operator::bit - 1; 0 <= j; --j) { if(ancestor[ancl.first][j].first!=ancestor[ancr.first][j].first) { ancl = Op.funcLca(ancestor[ancl.first][j],ancl); ancr = Op.funcLca(ancestor[ancr.first][j],ancr); } } ancl = Op.funcLca(ancestor[ancl.first][0],ancl); ancr = Op.funcLca(ancestor[ancr.first][0],ancr); return Op.funcLca(ancl,ancr); } //O(N) anytime int diameter(void){ makeDepth(0); int tmp = max_element(depth.begin(), depth.end()) - depth.begin(); makeDepth(tmp); return *max_element(depth.begin(), depth.end()); } //O(N^2) after makeDepth (include self) void makeDescendant(void) { descendant.resize(num); for (size_t i = 0; i < num; ++i) descendant[i].push_back(i); for (size_t i = 0; i < num; ++i) for (auto& e : edge[order[i]]) if (depth[order[i]] < depth[e.first]) for(auto k: descendant[e.first]) descendant[order[i]].push_back(k); } //O(N) after makeChild void makeSize(void) { size.resize(num,1); for (size_t i:order) for (auto e : child[i]) size[i] += size[e.first]; } //(N) after makeDepth and makeChild template vector rerooting(vector rerootdp,vector rerootparent) { for(size_t pa:order) for(auto& e:child[pa]) rerootdp[pa] = Op.funcReroot(rerootdp[pa],rerootdp[e.first]); for(size_t i = 0; i < num; ++i) { size_t pa = order[num - 1 - i]; if(depth[pa]) rerootdp[pa] = Op.funcReroot(rerootdp[pa],rerootparent[pa]); size_t m = child[pa].size(); for(int j = 0; j < m && depth[pa]; ++j){ size_t ch = child[pa][j].first; rerootparent[ch] = Op.funcReroot(rerootparent[ch],rerootparent[pa]); } if(m <= 1) continue; vector l(m),r(m); for(int j = 0; j < m; ++j) { size_t ch = child[pa][j].first; l[j] = rerootdp[ch]; r[j] = rerootdp[ch]; } for(int j = 1; j+1 < m; ++j) l[j] = Op.funcRerootMerge(l[j],l[j-1]); for(int j = m-2; 0 <=j; --j) r[j] = Op.funcRerootMerge(r[j],r[j+1]); size_t chl = child[pa].front().first; size_t chr = child[pa].back().first; rerootparent[chl] = Op.funcReroot(rerootparent[chl],r[1]); rerootparent[chr] = Op.funcReroot(rerootparent[chr],l[m-2]); for(int j = 1; j+1 < m; ++j) { size_t ch = child[pa][j].first; rerootparent[ch] = Op.funcReroot(rerootparent[ch],l[j-1]); rerootparent[ch] = Op.funcReroot(rerootparent[ch],r[j+1]); } } return rerootdp; } }; //depth,dist //https://atcoder.jp/contests/abc126/tasks/abc126_d //child //https://atcoder.jp/contests/abc133/tasks/abc133_e //lca //https://atcoder.jp/contests/abc014/tasks/abc014_4 //weighted lca //https://atcoder.jp/contests/code-thanks-festival-2017-open/tasks/code_thanks_festival_2017_h //https://atcoder.jp/contests/cf16-tournament-round1-open/tasks/asaporo_c //diameter //https://atcoder.jp/contests/agc033/tasks/agc033_c //descendant //https://atcoder.jp/contests/code-thanks-festival-2018/tasks/code_thanks_festival_2018_f //size //https://yukicoder.me/problems/no/872 //eulerTour //https://yukicoder.me/problems/no/900 template struct treeOperator{ static const size_t bit = 20; typeDist unitDist = 0; typeDist unitLca = 0; typeDist funcDist(const typeDist& parent,const typeDist& w){return parent+w;} pair funcLca(const pair& l,const pair& r){return make_pair(l.first,l.second+r.second);} template typeReroot funcReroot(const typeReroot& l,const typeReroot& r) { return {l.first+r.first+r.second,l.second+r.second}; } template typeReroot funcRerootMerge(const typeReroot& l,const typeReroot& r) { return {l.first+r.first,l.second+r.second}; } }; // Tree> tree(N); template ostream &operator<<(ostream &o, const map&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;} template ostream &operator<<(ostream &o, const set&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const multiset&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const vector&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template ostream &operator<<(ostream &o, const pair&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} template