File size: 4,118 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
#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> p;
vector<pair<int,int>> edges;
map<pair<int,int>, int> edgeIdx;
vector<int> parent, depth, in, out;
vector<vector<int>> children;
int timer;

bool in_subtree(int v, int x) {
    return in[v] <= in[x] && in[x] < out[v];
}

void dfs(int u, int par) {
    parent[u] = par;
    in[u] = timer++;
    for (int v : children[u]) {
        if (v == par) continue;
        depth[v] = depth[u] + 1;
        dfs(v, u);
    }
    out[u] = timer;
}

vector<int> get_upward_matching() {
    vector<pair<int,int>> cand;
    for (int v = 2; v <= n; ++v) {
        int u = parent[v];
        if (u == 0) continue;
        if (!in_subtree(v, p[v])) {
            cand.emplace_back(u, v);
        }
    }
    sort(cand.begin(), cand.end(), [](const pair<int,int>& a, const pair<int,int>& b) {
        return depth[a.second] > depth[b.second];
    });
    vector<bool> used(n+1, false);
    vector<int> matching;
    for (auto& e : cand) {
        int u = e.first, v = e.second;
        if (!used[u] && !used[v]) {
            used[u] = used[v] = true;
            matching.push_back(edgeIdx[{u, v}]);
        }
    }
    return matching;
}

vector<int> get_downward_matching() {
    vector<pair<int,int>> cand;
    for (int v = 2; v <= n; ++v) {
        int u = parent[v];
        if (u == 0) continue;
        if (in_subtree(v, p[u])) {
            cand.emplace_back(u, v);
        }
    }
    sort(cand.begin(), cand.end(), [](const pair<int,int>& a, const pair<int,int>& b) {
        return depth[a.second] > depth[b.second];
    });
    vector<bool> used(n+1, false);
    vector<int> matching;
    for (auto& e : cand) {
        int u = e.first, v = e.second;
        if (!used[u] && !used[v]) {
            used[u] = used[v] = true;
            matching.push_back(edgeIdx[{u, v}]);
        }
    }
    return matching;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        p.resize(n+1);
        for (int i = 1; i <= n; ++i) cin >> p[i];
        edges.resize(n);
        edgeIdx.clear();
        vector<vector<int>> adj(n+1);
        for (int i = 1; i < n; ++i) {
            int u, v;
            cin >> u >> v;
            edges[i] = {u, v};
            edgeIdx[{u, v}] = i;
            edgeIdx[{v, u}] = i;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        // prepare rooted tree
        parent.assign(n+1, 0);
        depth.assign(n+1, 0);
        in.assign(n+1, 0);
        out.assign(n+1, 0);
        children = adj; // temporary, will be refined
        // rebuild children lists (excluding parent)
        for (int i = 1; i <= n; ++i) children[i].clear();
        timer = 0;
        function<void(int,int)> dfs2 = [&](int u, int par) {
            parent[u] = par;
            in[u] = timer++;
            for (int v : adj[u]) {
                if (v == par) continue;
                depth[v] = depth[u] + 1;
                children[u].push_back(v);
                dfs2(v, u);
            }
            out[u] = timer;
        };
        dfs2(1, 0);

        vector<vector<int>> operations;
        bool up_next = true;
        while (true) {
            bool sorted = true;
            for (int i = 1; i <= n; ++i)
                if (p[i] != i) { sorted = false; break; }
            if (sorted) break;

            vector<int> matching;
            if (up_next) matching = get_upward_matching();
            else         matching = get_downward_matching();

            if (matching.empty()) {
                up_next = !up_next;   // try the other phase
                continue;
            }

            for (int idx : matching) {
                auto [u, v] = edges[idx];
                swap(p[u], p[v]);
            }
            operations.push_back(matching);
            up_next = !up_next;
        }

        cout << operations.size() << "\n";
        for (auto& op : operations) {
            cout << op.size();
            for (int idx : op) cout << " " << idx;
            cout << "\n";
        }
    }
    return 0;
}