#include using namespace std; using ll = long long; using pll = pair; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template struct edge{ int from; int to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} void reverse(){swap(from, to);} }; template struct edges : std::vector>{ void sort(){ std::sort( (*this).begin(), (*this).end(), [](const edge& a, const edge& b){ return a.cost < b.cost; } ); } }; template struct graph : std::vector>{ private: int n = 0; int m = 0; edges es; bool dir; public: graph(int n, bool dir) : n(n), dir(dir){ (*this).resize(n); } void add_edge(int from, int to, T cost=1){ if(dir){ es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m++)); }else{ if(from > to) swap(from, to); es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m)); (*this)[to].push_back(edge(to, from, cost, m++)); } } int get_vnum(){ return n; } int get_enum(){ return m; } bool get_dir(){ return dir; } edge get_edge(int i){ return es[i]; } edges get_edge_set(){ return es; } }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using weighted_graph = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; template struct lowest_common_ancestor{ int root = 0; int n; int log_n = 1; vector> par; vector depth; lowest_common_ancestor(){} lowest_common_ancestor(tree &T, int root=0) : root(root){ n = (int)T.size(); while((1 << log_n) < n) log_n++; par.resize(log_n, vector(n, -1)); depth.resize(n, 0); init(T); } void init(tree &T){ function dfs = [&](int v, int p){ par[0][v] = p; for(edge e : T[v]) if(e.to!=p){ depth[e.to] = depth[v]+1; dfs(e.to, v); } }; dfs(root, -1); for(int k=0; k+1> k & 1) u = par[k][u]; if(u == v) return u; for(int k=log_n-1; k>=0; k--){ if(par[k][u] != par[k][v]){ u = par[k][u]; v = par[k][v]; } } return par[0][u]; } }; template struct auxiliary_tree{ int n; vector in; int root; lowest_common_ancestor lca; auxiliary_tree(tree &T, int root = 0) : root(root){ n = (int)T.size(); in.resize(n); euler_tour(T); lca = lowest_common_ancestor(T, root); } void euler_tour(tree &T){ int now = 0; function dfs = [&](int v, int p){ in[v] = now++; for(edge e : T[v]) if(e.to != p) dfs(e.to, v); }; dfs(root, -1); } int build(int k, vector &vs, tree &AT){ sort(vs.begin(), vs.end(), [&](int i, int j){return in[i] < in[j];}); stack stk; stk.push(vs[0]); AT[vs[0]] = {}; for(int i=0; i(last)); last = stk.top(); stk.pop(); } if(stk.empty()||stk.top()!=r){ stk.push(r); vs.push_back(r); AT[r] = {edge(last)}; }else{ AT[r].push_back(edge(last)); } } stk.push(vs[i+1]); AT[vs[i+1]] = {}; } int r = stk.top(); while(!stk.empty()){ stk.pop(); if(!stk.empty()){ AT[stk.top()].push_back(edge(r)); r = stk.top(); } } function dfs = [&](int v, int p){ for(edge e : AT[v]) if(e.to != p){ AT[e.to].push_back(edge(v)); dfs(e.to, v); } }; dfs(r, -1); return r; } }; void solve(){ int n; cin >> n; tree T(n); vector> sdeg(n); vector deg(n); vector> es(n-1); for(int v=0; v> c; sdeg[v] = {c, v}; deg[v] = c; for(int i=0; i> id; id--; es[id].push_back(v); } } for(auto vec : es){ T[vec[0]].pb(vec[1]); T[vec[1]].pb(vec[0]); } sort(all(sdeg), greater>()); auxiliary_tree auxiliary_tree(T, 0); tree AT(n); // dp[v][0]:=頂点vで頂点vの親との辺を使わない // dp[v][1]:=頂点vで頂点vの親との辺を使う vector> dp(n, vector(2, 0)); for(int q=1; q vs; for(auto [d, v] : sdeg){ if(q <= d) vs.push_back(v); else break; } if(vs.empty()){ cout << 0 << " "; continue; } auxiliary_tree.build(vs.size(), vs, AT); function dfs = [&](int v, int p){ vector diff; int sum = 0; for(auto e : AT[v]) if(e.to != p){ dfs(e.to, v); diff.push_back(dp[e.to][0]-dp[e.to][1]); sum += dp[e.to][1]; } sort(all(diff), greater()); if(v == vs[0]){ // calc dp[0] if(q <= deg[v]){ dp[v][0] = 1 + sum; if(deg[v] - (int)AT[v].size() < q){ int rest = q - (deg[v] - (int)AT[v].size()); for(int i=0; i> T; while(T--) solve(); }