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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T;
    if (!(cin >> T)) return 0;
    while (T--) {
        int n;
        cin >> n;
        vector<int> p(n + 1);
        for (int i = 1; i <= n; ++i) cin >> p[i];

        vector<int> U(n), V(n); // 1..n-1 used
        vector<vector<pair<int,int>>> adj(n + 1);
        for (int i = 1; i <= n - 1; ++i) {
            int u, v;
            cin >> u >> v;
            U[i] = u; V[i] = v;
            adj[u].push_back({v, i});
            adj[v].push_back({u, i});
        }

        // Root the tree at 1 and compute parent, depth, and parent edge
        vector<int> parent(n + 1, 0), depth(n + 1, 0), pedge(n + 1, 0);
        vector<char> vis(n + 1, false);
        vector<int> q;
        q.push_back(1);
        vis[1] = true;
        parent[1] = 0; pedge[1] = 0; depth[1] = 0;
        for (int i = 0; i < (int)q.size(); ++i) {
            int u = q[i];
            for (auto [v, ei] : adj[u]) {
                if (!vis[v]) {
                    vis[v] = true;
                    parent[v] = u;
                    pedge[v] = ei;
                    depth[v] = depth[u] + 1;
                    q.push_back(v);
                }
            }
        }

        // Position of each label
        vector<int> pos(n + 1);
        for (int u = 1; u <= n; ++u) pos[p[u]] = u;

        // Remaining graph degrees and membership
        vector<int> deg(n + 1, 0);
        for (int u = 1; u <= n; ++u) deg[u] = (int)adj[u].size();
        vector<char> inR(n + 1, true);
        int rem = n;

        deque<int> leaves;
        for (int u = 1; u <= n; ++u) if (deg[u] <= 1) leaves.push_back(u);

        vector<int> ops;

        auto do_swap = [&](int e) {
            int a = U[e], b = V[e];
            ops.push_back(e);
            int va = p[a], vb = p[b];
            p[a] = vb; p[b] = va;
            pos[va] = b; pos[vb] = a;
        };

        auto move_token_along_path = [&](int start, int target) {
            int x = start, y = target;
            vector<int> up, down;
            while (depth[x] > depth[y]) { up.push_back(pedge[x]); x = parent[x]; }
            while (depth[y] > depth[x]) { down.push_back(pedge[y]); y = parent[y]; }
            while (x != y) {
                up.push_back(pedge[x]); x = parent[x];
                down.push_back(pedge[y]); y = parent[y];
            }
            for (int e : up) do_swap(e);
            for (int i = (int)down.size() - 1; i >= 0; --i) do_swap(down[i]);
        };

        while (rem > 1) {
            while (!leaves.empty() && (!inR[leaves.front()] || deg[leaves.front()] != 1)) leaves.pop_front();
            if (leaves.empty()) {
                for (int u = 1; u <= n; ++u)
                    if (inR[u] && deg[u] == 1) leaves.push_back(u);
                if (leaves.empty()) break;
            }
            int v = leaves.front(); leaves.pop_front();
            if (!inR[v] || deg[v] != 1) continue;

            int cur = pos[v];
            if (cur != v) move_token_along_path(cur, v);

            inR[v] = false;
            --rem;

            int neighbor = 0;
            for (auto [to, ei] : adj[v]) if (inR[to]) { neighbor = to; break; }
            deg[v] = 0;
            if (neighbor) {
                --deg[neighbor];
                if (deg[neighbor] == 1) leaves.push_back(neighbor);
            }
        }

        cout << ops.size() << "\n";
        for (int e : ops) {
            cout << 1 << " " << e << "\n";
        }
    }

    return 0;
}