#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 diameter{ int n; int s=0, t=0; vector path; S diam = 0; diameter(graph &T){ n = (int)T.size(); vector dist1(n, 0); function dfs1 = [&](int v, int p){ for(auto e : T[v]) if(e.to!=p){ dist1[e.to] = dist1[v] + e.cost; dfs1(e.to, v); } }; dfs1(0, -1); S sdist = 0; for(int v=0; v dist2(n, 0); function dfs2 = [&](int v, int p){ for(auto e : T[v]) if(e.to!=p){ dist2[e.to] = dist2[v] + e.cost; dfs2(e.to, v); } }; dfs2(s, -1); S tdist = 0; for(int v=0; v dfs3 = [&](int v, int p){ if(v == t){ path.pb(v); return true; } bool flag = false; for(auto e : T[v]) if(e.to!=p){ flag = flag | dfs3(e.to, v); } if(flag) path.pb(v); return flag; }; dfs3(s, -1); } pair get_endpoints(){return {s, t};} vector get_path(){return path;} S get_distance(){return diam;} }; void solve(){ int n; cin >> n; graph T(n, false); for(int i=0; i> u >> v; u--; v--; T.add_edge(u, v); } diameter diam(T); auto [s, t] = diam.get_endpoints(); if(s > t) swap(s, t); auto P = diam.get_path(); int D = diam.get_distance(); vector ePath; for(int i=0; i&)> calc_dist = [&](int v, int p, vector &dist){ for(auto e : T[v]) if(e.to!=p){ dist[e.to] = dist[v] + 1; calc_dist(e.to, v, dist); } }; vector sdist(n, 0), tdist(n, 0); calc_dist(s, -1, sdist); calc_dist(t, -1, tdist); vector onP(n, false); for(int v : P) onP[v] = true; vector ans(n-1, 0); ll U = 0; for(int i=0; i> anc(n); // Case1 : P上にない辺の答えを計算 { vector siz(n, 1); function dfs = [&](int v, int p, int r){ anc[r].pb(v); for(auto e : T[v]) if(e.to!=p && !onP[e.to]){ dfs(e.to, v, r); ans[e.id] = U - (siz[e.to]-1) - max(sdist[e.to], tdist[e.to]); siz[v] += siz[e.to]; } }; for(int v : P) dfs(v, -1, v); } // Case2 : D is even if(D%2 == 0){ // Case2-1 : e_0, e_1, ..., e_(D/2-1)を縮約 { vector d(D+1, 0); for(int i=D-1; i>=0; i--){ d[i] = d[i+1]; for(int v : anc[P[i]]) d[i] = max(d[i], tdist[v]); } // 前から探索 int lsum = 0; int rsum = 0; for(int i=D; i>D/2; i--) rsum += anc[P[i]].size(); for(int i=0; i d(D+1, 0); for(int i=1; i<=D; i++){ d[i] = d[i-1]; for(int v : anc[P[i]]) d[i] = max(d[i], sdist[v]); } // 後ろから探索 int lsum = 0; for(int i=0; i=D/2; i--){ // 辺ePath[i]を縮約 rsum += anc[P[i+1]].size(); if(d[i] == D){ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]]; }else{ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]] - lsum; } } } } // Case3 : D is odd if(D%2 == 1){ // Case3-1 : e_0, e_1, ..., e_(D/2-1)を縮約 { vector d(D+1, 0); for(int i=D-1; i>=0; i--){ d[i] = d[i+1]; for(int v : anc[P[i]]) d[i] = max(d[i], tdist[v]); } // 前から探索 int lsum = 0; int rsum = 0; for(int i=D; i>D/2; i--) rsum += anc[P[i]].size(); for(int i=0; i d(D+1, 0); for(int i=1; i<=D; i++){ d[i] = d[i-1]; for(int v : anc[P[i]]) d[i] = max(d[i], sdist[v]); } // 後ろから探索 int lsum = 0; for(int i=0; i<=D/2; i++) lsum += anc[P[i]].size(); int rsum = 0; for(int i=D-1; i>=D/2+1; i--){ // 辺ePath[i]を縮約 rsum += anc[P[i+1]].size(); if(d[i] == D){ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]]; }else{ ans[ePath[i]] = U - (rsum-1) - sdist[P[i+1]] - lsum; } } } } for(int i=0; i> T; while(T--) solve(); }