#define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/stack:16777216") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i) #define REP(i, N) FOR(i, 0, N) #define RREP(i, N) RFOR(i, N, 0) #define FILL(A,value) memset(A,value,sizeof(A)) #define ALL(V) V.begin(), V.end() #define SZ(V) (int)V.size() #define PB push_back #define MP make_pair #define Pi 3.14159265358979 typedef long long Int; typedef unsigned long long UINT; typedef vector VI; typedef pair PII; const int INF = 2000000000; const int MAX = 20000; const int MAX2 = 1007; const int BASE = 1000000000; const int MOD = 1000000007; struct test { int n; VI U; VI V; test(){} void Write() { cout << n << endl; FOR (i,0,n-1) { cout << U[i]+1 << ' ' << V[i]+1 << endl; } } void Read() { cin >> n; FOR (i,0,n-1) { int u, v; cin >> u >> v; U.PB(u-1); V.PB(v-1); } } VI G[MAX]; vector A; void dfs(int v, int p, int h) { A.PB(MP(h, v)); FOR (i,0,SZ(G[v])) { int to = G[v][i]; if (to == p) continue; dfs(to, v, h+1); } } void Solve() { FOR (i,0,n-1) { G[V[i]].PB(U[i]); G[U[i]].PB(V[i]); } A.clear(); dfs(0, -1, 0); sort(ALL(A)); int level = 0, l = 0, r = SZ(A)-1, cnt = 0; while (l <= r) { if (cnt % 2 == 0) { while (l < SZ(A) && A[l].first == level) ++ l; ++ level; } else { -- r; } ++ cnt; } cout << cnt << endl; } }; test Get1(int n) { test res; res.n = n; vector E; VI P; FOR (i,0,n) P.PB(i); random_shuffle(P.begin()+1, P.end()); FOR (i,1,n) { int v = rand() % i; E.PB(MP(P[v], P[i])); } FOR (i,0,SZ(E)) if (rand() % 2) swap(E[i].first, E[i].second); random_shuffle(ALL(E)); FOR (i,0,SZ(E)) { res.U.PB(E[i].first); res.V.PB(E[i].second); } return res; } test Get2(int n, int m) { test res; res.n = n; vector E; VI P; FOR (i,0,n) P.PB(i); random_shuffle(P.begin()+1, P.end()); int val = rand() % m + 1; FOR (i,1,n) { int v = rand() % min(i, val); E.PB(MP(P[v], P[i])); } FOR (i,0,SZ(E)) if (rand() % 2) swap(E[i].first, E[i].second); random_shuffle(ALL(E)); FOR (i,0,SZ(E)) { res.U.PB(E[i].first); res.V.PB(E[i].second); } return res; } test Get3(int n, int m) { test res; res.n = n; vector E; VI P; FOR (i,0,n) P.PB(i); random_shuffle(P.begin()+1, P.end()); int val = rand() % m + 1; FOR (i,1,n) { int v = max(0, i - rand() % val - 1); E.PB(MP(P[v], P[i])); } FOR (i,0,SZ(E)) if (rand() % 2) swap(E[i].first, E[i].second); random_shuffle(ALL(E)); FOR (i,0,SZ(E)) { res.U.PB(E[i].first); res.V.PB(E[i].second); } return res; } void OpenInput(int cnt) { char buf[256]; sprintf(buf, "D:\\witua\\Cook-off August\\TREEGAME\\%d.in", cnt); freopen(buf, "wb", stdout); } void OpenOutput(int cnt) { char buf[256]; sprintf(buf, "D:\\witua\\Cook-off August\\TREEGAME\\%d.out", cnt); freopen(buf, "wb", stdout); } void WriteInput(vector A) { cout << SZ(A) << endl; FOR (i,0,SZ(A)) { A[i].Write(); } } void WriteOutput(vector A) { FOR (i,0,SZ(A)) A[i].Solve(); } void Do(vector A, int cnt) { OpenInput(cnt); WriteInput(A); OpenOutput(cnt); WriteOutput(A); } int main() { int cnt; cin >> cnt; FOR (i,0,cnt) { test t; t.Read(); t.Solve(); } /*int cnt = 0; vector A; A.clear(); FOR (i,0,10) A.PB(Get1(100)); Do(A, cnt ++); A.clear(); FOR (i,0,10) A.PB(Get1(1000)); Do(A, cnt ++); A.clear(); FOR (i,0,3) A.PB(Get2(1000, 100)); FOR (i,0,3) A.PB(Get2(1000, 10)); FOR (i,0,3) A.PB(Get2(1000, 5)); FOR (i,0,1) A.PB(Get2(1000, 1)); Do(A, cnt ++); A.clear(); FOR (i,0,3) A.PB(Get3(1000, 100)); FOR (i,0,3) A.PB(Get3(1000, 10)); FOR (i,0,3) A.PB(Get3(1000, 5)); FOR (i,0,1) A.PB(Get3(1000, 1)); Do(A, cnt ++);*/ return 0; }