#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RFOR(i,p,n) for(int i=(p);i>=(n);--i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define gc getchar_unlocked
#define pc putchar_unlocked
#define pn putchar_unlocked('\n')
using namespace std;
typedef long long ll;
typedef __int128_t lll;
typedef V<int> vi;
typedef V<ll> vll;
typedef const int ci;
typedef const ll cll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf = 2.1e9;
cll infll = 4.5e18;
int I(){
int z;
int c = gc();
while(c < '0' || c > '9') c = gc();
for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0';
return z;
}
void O(int a){
static int tab[20];
int it = 0;
do{
tab[it++] = int('0'+a%10);
} while(a /= 10);
while(it) pc(tab[--it]);
}
mt19937 mt(21376969);
int los(ci a, ci b){
uniform_int_distribution<int> dist(a, b);
return dist(mt);
}
void answer(){
ci n = I();
V<vi> g(n+1);
REP(i, n-1){
ci a = I();
ci b = I();
g[a].eb(b);
g[b].eb(a);
}
vi wyn = {n+1};
struct stan{
vi kod;
vi nr;
int iter;
bool operator==(C stan&) const = default;
};
deque<stan> stany;
auto wrzuc = [&](stan s){
if(ssize(stany)) REP(i, min(ssize(s.kod), ssize(stany.back().kod))){
if(s.kod[i] > stany.back().kod[i]) return;
if(s.kod[i] < stany.back().kod[i]){
while(ssize(stany) && i < ssize(stany.back().kod)) stany.pop_back();
break;
}
}
if(ssize(stany) && s == stany.back()) return;
if(ssize(s.kod) == n-2) return chmin(wyn, s.kod);
stany.eb(s);
if(ssize(stany) > 100'000) erase_if(stany, [&](C stan&){return los(0, 1);});
};
auto przedluz = [&](stan s){
vi st(n+1, 0);
FOR(w, 1, n) if(s.nr[w]>=0) for(ci i : g[w]) st[w] += s.nr[i]>=0;
FOR(ww, 1, n) if(int w = ww; s.nr[w]>=0){
int liscie = 0;
for(ci i : g[w]) if(st[i] == 1) ++liscie;
if(!liscie) continue;
stan nowy = s;
vi nowy_st = st;
while(true){
if(!nowy.nr[w]) nowy.nr[w] = ++nowy.iter;
for(ci i : g[w]) if(ssize(nowy.kod) < n-2 && nowy_st[i] == 1){
nowy.kod.eb(nowy.nr[w]);
nowy.nr[i] = -1;
nowy_st[i] = 0;
--nowy_st[w];
}
if(ssize(nowy.kod) < n-2 && nowy_st[w] == 1){
for(ci i : g[w]) if(nowy.nr[i]>=0){
w = i;
break;
}
continue;
}
break;
}
wrzuc(nowy);
}
};
wrzuc(stan{vi{}, vi(n+1, 0), 0});
while(ssize(stany)){
stan s = stany.front();
stany.pop_front();
przedluz(s);
}
for(ci x : wyn) O(x), pc(' ');
pn;
}
int main(){
int tt = I();
while(tt--) answer();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define RFOR(i,p,n) for(int i=(p);i>=(n);--i) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define ssize(x) int((x).size()) #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define C const #define gc getchar_unlocked #define pc putchar_unlocked #define pn putchar_unlocked('\n') using namespace std; typedef long long ll; typedef __int128_t lll; typedef V<int> vi; typedef V<ll> vll; typedef const int ci; typedef const ll cll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; void chmin(auto &a, auto b){a=min(a,b);} void chmax(auto &a, auto b){a=max(a,b);} ci inf = 2.1e9; cll infll = 4.5e18; int I(){ int z; int c = gc(); while(c < '0' || c > '9') c = gc(); for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0'; return z; } void O(int a){ static int tab[20]; int it = 0; do{ tab[it++] = int('0'+a%10); } while(a /= 10); while(it) pc(tab[--it]); } mt19937 mt(21376969); int los(ci a, ci b){ uniform_int_distribution<int> dist(a, b); return dist(mt); } void answer(){ ci n = I(); V<vi> g(n+1); REP(i, n-1){ ci a = I(); ci b = I(); g[a].eb(b); g[b].eb(a); } vi wyn = {n+1}; struct stan{ vi kod; vi nr; int iter; bool operator==(C stan&) const = default; }; deque<stan> stany; auto wrzuc = [&](stan s){ if(ssize(stany)) REP(i, min(ssize(s.kod), ssize(stany.back().kod))){ if(s.kod[i] > stany.back().kod[i]) return; if(s.kod[i] < stany.back().kod[i]){ while(ssize(stany) && i < ssize(stany.back().kod)) stany.pop_back(); break; } } if(ssize(stany) && s == stany.back()) return; if(ssize(s.kod) == n-2) return chmin(wyn, s.kod); stany.eb(s); if(ssize(stany) > 100'000) erase_if(stany, [&](C stan&){return los(0, 1);}); }; auto przedluz = [&](stan s){ vi st(n+1, 0); FOR(w, 1, n) if(s.nr[w]>=0) for(ci i : g[w]) st[w] += s.nr[i]>=0; FOR(ww, 1, n) if(int w = ww; s.nr[w]>=0){ int liscie = 0; for(ci i : g[w]) if(st[i] == 1) ++liscie; if(!liscie) continue; stan nowy = s; vi nowy_st = st; while(true){ if(!nowy.nr[w]) nowy.nr[w] = ++nowy.iter; for(ci i : g[w]) if(ssize(nowy.kod) < n-2 && nowy_st[i] == 1){ nowy.kod.eb(nowy.nr[w]); nowy.nr[i] = -1; nowy_st[i] = 0; --nowy_st[w]; } if(ssize(nowy.kod) < n-2 && nowy_st[w] == 1){ for(ci i : g[w]) if(nowy.nr[i]>=0){ w = i; break; } continue; } break; } wrzuc(nowy); } }; wrzuc(stan{vi{}, vi(n+1, 0), 0}); while(ssize(stany)){ stan s = stany.front(); stany.pop_front(); przedluz(s); } for(ci x : wyn) O(x), pc(' '); pn; } int main(){ int tt = I(); while(tt--) answer(); } |
English