#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct state
{
vector<bool> del;
vi set_num;
int cur_max;
ll hash()
{
ll h = cur_max;
ll h2 = cur_max;
ll p = 2137;
ll p2 = 2137001;
rep(i,siz(del))
{
h = (h+(ll)del[i]*p)%MOD;
h2 = (h2+(ll)del[i]*p2)%MOD;
p = (p*2137)%MOD;
p2 = (p2*2137001)%MOD;
}
rep(i,siz(set_num))
{
h = (h+(ll)(set_num[i]+1)*p)%MOD;
h2 = (h2+(ll)(set_num[i]+1)*p2)%MOD;
p = (p*2137)%MOD;
p2 = (p2*2137001)%MOD;
}
return h+h2*MOD;
}
};
int n;
vi graph[1001];
unordered_map<ll,vi> ans_map;
vi rek(state s)
{
ll h = s.hash();
if(ans_map.find(h) != ans_map.end()) return ans_map[h];
int ile = 0;
rep(i,n) if(s.del[i] == 0) ile++;
if(ile <= 2)
{
ans_map[h] = {};
return ans_map[h];
}
// set leaf
pii min_ = {1e9,-1};
rep(i,n)
{
if(s.del[i] == 1 || s.set_num[i] == -1) continue;
int cnt = 0;
forall(it,graph[i]) if(!s.del[it]) cnt++;
if(cnt == 1) min_ = min(min_,{s.set_num[i],i});
}
if(min_.ss != -1)
{
int v = 0;
forall(it,graph[min_.ss]) if(!s.del[it]) v = it;
if(s.set_num[v] == -1)
{
s.set_num[v] = s.cur_max;
s.cur_max++;
s.del[min_.ss] = 1;
ans_map[h] = {s.cur_max-1};
vi pom = rek(s);
forall(it,pom) ans_map[h].pb(it);
return ans_map[h];
}
else
{
s.del[min_.ss] = 1;
ans_map[h] = {s.set_num[v]};
vi pom = rek(s);
forall(it,pom) ans_map[h].pb(it);
return ans_map[h];
}
}
// set hub
min_ = {1e9,-1};
rep(i,n)
{
if(s.del[i] == 1 || s.set_num[i] == -1) continue;
int cnt = 0;
forall(it,graph[i])
{
if(!s.del[it])
{
int cnt2 = 0;
forall(it2,graph[it])
{
if(!s.del[it2])
{
cnt2++;
if(cnt2 == 2) break;
}
}
if(cnt2 <= 1)
{
cnt++;
break;
}
}
}
if(cnt >= 1) min_ = min(min_,{s.set_num[i],i});
}
if(min_.ss != -1)
{
forall(it,graph[min_.ss])
{
if(!s.del[it])
{
int cnt2 = 0;
forall(it2,graph[it])
{
if(!s.del[it2])
{
cnt2++;
if(cnt2 == 2) break;
}
}
if(cnt2 <= 1)
{
if(ile != 2)
{
ans_map[h].pb(s.set_num[min_.ss]);
s.del[it] = 1;
}
ile--;
}
}
}
vi pom = rek(s);
forall(it,pom) ans_map[h].pb(it);
return ans_map[h];
}
// rand hub
rep(i,n)
{
if(s.del[i] == 1) continue;
int cnt = 0;
forall(it,graph[i])
{
if(!s.del[it])
{
int cnt2 = 0;
forall(it2,graph[it])
{
if(!s.del[it2])
{
cnt2++;
if(cnt2 == 2) break;
}
}
if(cnt2 <= 1)
{
cnt++;
break;
}
}
}
if(cnt == 0) continue;
s.set_num[i] = s.cur_max;
s.cur_max++;
if(ans_map.find(h) == ans_map.end()) ans_map[h] = rek(s);
else ans_map[h] = min(ans_map[h],rek(s));
s.set_num[i] = -1;
s.cur_max--;
}
return ans_map[h];
}
void solve()
{
cin >> n;
rep(i,n) graph[i] = {};
ans_map = {};
rep(i,n-1)
{
int a,b;
cin >> a >> b;
a--;
b--;
graph[a].pb(b);
graph[b].pb(a);
}
state s;
s.del.resize(n,0);
s.set_num.resize(n,-1);
s.cur_max = 1;
vi ans = rek(s);
forall(it,ans) cout << it << " ";
cout << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
int t = 1;
cin >> t;
while(t--) solve();
}
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | #include <bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define ull unsigned long long #define ff first #define ss second #define pii pair<int,int> #define pll pair<long long, long long> #define vi vector<int> #define vl vector<long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; //mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll los(ll a, ll b) {return a + (mt() % (b-a+1));} const int INF = 1e9+50; const ll INF_L = 1e18+40; const ll MOD = 1e9+7; struct state { vector<bool> del; vi set_num; int cur_max; ll hash() { ll h = cur_max; ll h2 = cur_max; ll p = 2137; ll p2 = 2137001; rep(i,siz(del)) { h = (h+(ll)del[i]*p)%MOD; h2 = (h2+(ll)del[i]*p2)%MOD; p = (p*2137)%MOD; p2 = (p2*2137001)%MOD; } rep(i,siz(set_num)) { h = (h+(ll)(set_num[i]+1)*p)%MOD; h2 = (h2+(ll)(set_num[i]+1)*p2)%MOD; p = (p*2137)%MOD; p2 = (p2*2137001)%MOD; } return h+h2*MOD; } }; int n; vi graph[1001]; unordered_map<ll,vi> ans_map; vi rek(state s) { ll h = s.hash(); if(ans_map.find(h) != ans_map.end()) return ans_map[h]; int ile = 0; rep(i,n) if(s.del[i] == 0) ile++; if(ile <= 2) { ans_map[h] = {}; return ans_map[h]; } // set leaf pii min_ = {1e9,-1}; rep(i,n) { if(s.del[i] == 1 || s.set_num[i] == -1) continue; int cnt = 0; forall(it,graph[i]) if(!s.del[it]) cnt++; if(cnt == 1) min_ = min(min_,{s.set_num[i],i}); } if(min_.ss != -1) { int v = 0; forall(it,graph[min_.ss]) if(!s.del[it]) v = it; if(s.set_num[v] == -1) { s.set_num[v] = s.cur_max; s.cur_max++; s.del[min_.ss] = 1; ans_map[h] = {s.cur_max-1}; vi pom = rek(s); forall(it,pom) ans_map[h].pb(it); return ans_map[h]; } else { s.del[min_.ss] = 1; ans_map[h] = {s.set_num[v]}; vi pom = rek(s); forall(it,pom) ans_map[h].pb(it); return ans_map[h]; } } // set hub min_ = {1e9,-1}; rep(i,n) { if(s.del[i] == 1 || s.set_num[i] == -1) continue; int cnt = 0; forall(it,graph[i]) { if(!s.del[it]) { int cnt2 = 0; forall(it2,graph[it]) { if(!s.del[it2]) { cnt2++; if(cnt2 == 2) break; } } if(cnt2 <= 1) { cnt++; break; } } } if(cnt >= 1) min_ = min(min_,{s.set_num[i],i}); } if(min_.ss != -1) { forall(it,graph[min_.ss]) { if(!s.del[it]) { int cnt2 = 0; forall(it2,graph[it]) { if(!s.del[it2]) { cnt2++; if(cnt2 == 2) break; } } if(cnt2 <= 1) { if(ile != 2) { ans_map[h].pb(s.set_num[min_.ss]); s.del[it] = 1; } ile--; } } } vi pom = rek(s); forall(it,pom) ans_map[h].pb(it); return ans_map[h]; } // rand hub rep(i,n) { if(s.del[i] == 1) continue; int cnt = 0; forall(it,graph[i]) { if(!s.del[it]) { int cnt2 = 0; forall(it2,graph[it]) { if(!s.del[it2]) { cnt2++; if(cnt2 == 2) break; } } if(cnt2 <= 1) { cnt++; break; } } } if(cnt == 0) continue; s.set_num[i] = s.cur_max; s.cur_max++; if(ans_map.find(h) == ans_map.end()) ans_map[h] = rek(s); else ans_map[h] = min(ans_map[h],rek(s)); s.set_num[i] = -1; s.cur_max--; } return ans_map[h]; } void solve() { cin >> n; rep(i,n) graph[i] = {}; ans_map = {}; rep(i,n-1) { int a,b; cin >> a >> b; a--; b--; graph[a].pb(b); graph[b].pb(a); } state s; s.del.resize(n,0); s.set_num.resize(n,-1); s.cur_max = 1; vi ans = rek(s); forall(it,ans) cout << it << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //random_start(); int t = 1; cin >> t; while(t--) solve(); } |
English