File size: 7,067 Bytes
1fd0050 | 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | #include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 1005;
const int LOGN = 11;
int n;
vector<int> adj[MAXN];
map<pair<int, int>, int> edge_to_idx;
int p[MAXN];
int pos[MAXN];
int depth[MAXN];
int parent[MAXN];
int up[MAXN][LOGN];
void bfs(int root) {
for (int i = 1; i <= n; ++i) {
depth[i] = -1;
parent[i] = 0;
}
vector<int> q;
q.push_back(root);
depth[root] = 0;
int head = 0;
while(head < q.size()){
int u = q[head++];
for(int v : adj[u]){
if(depth[v] == -1){
depth[v] = depth[u] + 1;
parent[v] = u;
q.push_back(v);
}
}
}
}
void build_lca() {
for (int i = 1; i <= n; ++i) {
up[i][0] = parent[i];
}
for (int j = 1; j < LOGN; ++j) {
for (int i = 1; i <= n; ++i) {
up[i][j] = up[up[i][j-1]][j-1];
}
}
}
int get_ancestor(int u, int k) {
for (int i = 0; i < LOGN; ++i) {
if ((k >> i) & 1) {
u = up[u][i];
}
}
return u;
}
int lca(int u, int v) {
if (u == 0 || v == 0) return 0;
if (depth[u] < depth[v]) swap(u, v);
u = get_ancestor(u, depth[u] - depth[v]);
if (u == v) return u;
for (int i = LOGN - 1; i >= 0; --i) {
if (up[u][i] != up[v][i]) {
u = up[u][i];
v = up[v][i];
}
}
return parent[u];
}
void apply_swaps(const vector<pair<int, int>>& matching) {
for (auto const& edge : matching) {
int u = edge.first;
int v = edge.second;
int val_u = p[u];
int val_v = p[v];
swap(p[u], p[v]);
pos[val_u] = v;
pos[val_v] = u;
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
adj[i].clear();
}
edge_to_idx.clear();
for (int i = 1; i <= n; ++i) {
cin >> p[i];
pos[p[i]] = i;
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
if (u > v) swap(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
edge_to_idx[{u, v}] = i + 1;
}
bfs(1);
build_lca();
int max_depth = 0;
for (int i = 1; i <= n; ++i) {
max_depth = max(max_depth, depth[i]);
}
vector<vector<int>> operations;
// Uphill phase
for (int iter = 0; iter <= max_depth; ++iter) {
// Odd depth children
vector<pair<int, int>> matching_odd;
vector<bool> parent_used(n + 1, false);
for (int u = 1; u <= n; ++u) {
if (parent[u] != 0 && depth[u] % 2 != 0) {
if (parent_used[parent[u]]) continue;
int k = p[u];
if (pos[k] != k && lca(pos[k], k) != pos[k]) {
matching_odd.push_back({u, parent[u]});
parent_used[parent[u]] = true;
}
}
}
if (!matching_odd.empty()) {
vector<int> op;
op.push_back(matching_odd.size());
for (auto const& edge : matching_odd) {
int u = edge.first, v = edge.second;
if (u > v) swap(u,v);
op.push_back(edge_to_idx.at({u,v}));
}
operations.push_back(op);
apply_swaps(matching_odd);
}
// Even depth children
vector<pair<int, int>> matching_even;
parent_used.assign(n + 1, false);
for (int u = 1; u <= n; ++u) {
if (parent[u] != 0 && depth[u] % 2 == 0) {
if (parent_used[parent[u]]) continue;
int k = p[u];
if (pos[k] != k && lca(pos[k], k) != pos[k]) {
matching_even.push_back({u, parent[u]});
parent_used[parent[u]] = true;
}
}
}
if (!matching_even.empty()) {
vector<int> op;
op.push_back(matching_even.size());
for (auto const& edge : matching_even) {
int u = edge.first, v = edge.second;
if (u > v) swap(u,v);
op.push_back(edge_to_idx.at({u,v}));
}
operations.push_back(op);
apply_swaps(matching_even);
}
}
// Downhill phase
for (int iter = 0; iter <= max_depth; ++iter) {
// Odd depth parents
vector<pair<int, int>> matching_odd;
vector<bool> node_used(n + 1, false);
for (int u = 1; u <= n; ++u) {
if (depth[u] % 2 != 0) {
if (node_used[u]) continue;
int k = p[u];
if (pos[k] != k && lca(pos[k], k) == pos[k]) {
if (depth[k] - depth[u] <= 0) continue;
int child = get_ancestor(k, depth[k] - depth[u] - 1);
if (!node_used[child]) {
matching_odd.push_back({u, child});
node_used[u] = true;
node_used[child] = true;
}
}
}
}
if (!matching_odd.empty()) {
vector<int> op;
op.push_back(matching_odd.size());
for (auto const& edge : matching_odd) {
int u = edge.first, v = edge.second;
if (u > v) swap(u,v);
op.push_back(edge_to_idx.at({u,v}));
}
operations.push_back(op);
apply_swaps(matching_odd);
}
// Even depth parents
vector<pair<int, int>> matching_even;
node_used.assign(n + 1, false);
for (int u = 1; u <= n; ++u) {
if (depth[u] % 2 == 0) {
if (node_used[u]) continue;
int k = p[u];
if (pos[k] != k && lca(pos[k], k) == pos[k]) {
if (depth[k] - depth[u] <= 0) continue;
int child = get_ancestor(k, depth[k] - depth[u] - 1);
if (!node_used[child]) {
matching_even.push_back({u, child});
node_used[u] = true;
node_used[child] = true;
}
}
}
}
if (!matching_even.empty()) {
vector<int> op;
op.push_back(matching_even.size());
for (auto const& edge : matching_even) {
int u = edge.first, v = edge.second;
if (u > v) swap(u,v);
op.push_back(edge_to_idx.at({u,v}));
}
operations.push_back(op);
apply_swaps(matching_even);
}
}
cout << operations.size() << "\n";
for (const auto& op : operations) {
for (int i = 0; i < op.size(); ++i) {
cout << op[i] << (i == op.size() - 1 ? "" : " ");
}
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
} |