#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
#ifdef LOCAL
template<typename A, typename B>
auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
using i64 = long long;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<i64, i64>;
using vi = vector<int>;
using vll = vector<i64>;
struct Testcase {
vector<vi> graph;
int N;
vector<vi> leaf_neighbor_info;
vi leaves;
vi leaf_neighbors;
void Input() {
cin >> N;
graph.resize(N + 1);
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
}
void LeafInit() {
leaf_neighbor_info.resize(N + 1);
for (int v = 1; v <= N; ++v) {
if (SZ(graph[v]) == 1) {
leaves.push_back(v);
leaf_neighbor_info[graph[v][0]].push_back(v);
}
}
for (int v = 1; v <= N; ++v) {
if (!leaf_neighbor_info[v].empty()) {
leaf_neighbors.push_back(v);
}
}
debug(leaf_neighbors);
}
vi Simulate() {
vi prufer;
vi mapping(N + 1, -1);
priority_queue<pii, vector<pii>, greater<pii>> leaf_pq;
int last_idx = N;
int first_idx = 1;
vi cur_deg(N + 1);
vector<bool> is_removed(N + 1);
for (int v = 1; v <= N; ++v) {
cur_deg[v] = SZ(graph[v]);
}
for (int v : leaf_neighbors) {
for (int x : leaf_neighbor_info[v]) {
assert(cur_deg[x] == 1);
mapping[x] = last_idx;
--last_idx;
leaf_pq.emplace(mapping[x], x);
}
}
debug(mapping);
while (SZ(prufer) < N - 2) {
const int v = leaf_pq.top().second;
debug(v);
leaf_pq.pop();
assert(cur_deg[v] == 1);
is_removed[v] = true;
for (int s : graph[v]) {
if (is_removed[s]) { continue; }
if (mapping[s] == -1) {
mapping[s] = first_idx;
++first_idx;
}
prufer.push_back(mapping[s]);
--cur_deg[s];
if (cur_deg[s] == 1) {
leaf_pq.emplace(mapping[s], s);
}
}
}
debug(leaf_neighbors, mapping, prufer);
return prufer;
}
void Run() {
Input();
LeafInit();
vi ans{N + 1};
do {
ans = min(ans, Simulate());
} while (next_permutation(ALL(leaf_neighbors)));
for (int i = 0; i < N - 2; ++i) {
if (i) { cout << " "; }
cout << ans[i];
}
cout << "\n";
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(14);
cerr << fixed << setprecision(6);
int T;
cin >> T;
for (int tidx = 0; tidx < T; ++tidx) {
Testcase{}.Run();
}
}
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 | #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) using namespace std; #ifdef LOCAL template<typename A, typename B> auto&operator<<(auto&o,pair<A, B>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<&","[!i++]<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif using i64 = long long; using ll = long long; using pii = pair<int, int>; using pll = pair<i64, i64>; using vi = vector<int>; using vll = vector<i64>; struct Testcase { vector<vi> graph; int N; vector<vi> leaf_neighbor_info; vi leaves; vi leaf_neighbors; void Input() { cin >> N; graph.resize(N + 1); for (int i = 0; i < N - 1; ++i) { int u, v; cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } } void LeafInit() { leaf_neighbor_info.resize(N + 1); for (int v = 1; v <= N; ++v) { if (SZ(graph[v]) == 1) { leaves.push_back(v); leaf_neighbor_info[graph[v][0]].push_back(v); } } for (int v = 1; v <= N; ++v) { if (!leaf_neighbor_info[v].empty()) { leaf_neighbors.push_back(v); } } debug(leaf_neighbors); } vi Simulate() { vi prufer; vi mapping(N + 1, -1); priority_queue<pii, vector<pii>, greater<pii>> leaf_pq; int last_idx = N; int first_idx = 1; vi cur_deg(N + 1); vector<bool> is_removed(N + 1); for (int v = 1; v <= N; ++v) { cur_deg[v] = SZ(graph[v]); } for (int v : leaf_neighbors) { for (int x : leaf_neighbor_info[v]) { assert(cur_deg[x] == 1); mapping[x] = last_idx; --last_idx; leaf_pq.emplace(mapping[x], x); } } debug(mapping); while (SZ(prufer) < N - 2) { const int v = leaf_pq.top().second; debug(v); leaf_pq.pop(); assert(cur_deg[v] == 1); is_removed[v] = true; for (int s : graph[v]) { if (is_removed[s]) { continue; } if (mapping[s] == -1) { mapping[s] = first_idx; ++first_idx; } prufer.push_back(mapping[s]); --cur_deg[s]; if (cur_deg[s] == 1) { leaf_pq.emplace(mapping[s], s); } } } debug(leaf_neighbors, mapping, prufer); return prufer; } void Run() { Input(); LeafInit(); vi ans{N + 1}; do { ans = min(ans, Simulate()); } while (next_permutation(ALL(leaf_neighbors))); for (int i = 0; i < N - 2; ++i) { if (i) { cout << " "; } cout << ans[i]; } cout << "\n"; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(14); cerr << fixed << setprecision(6); int T; cin >> T; for (int tidx = 0; tidx < T; ++tidx) { Testcase{}.Run(); } } |
English