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

struct FastScanner {
    static constexpr size_t BUFSIZE = 1 << 20;
    int idx = 0, size = 0;
    char buf[BUFSIZE];

    inline char readChar() {
        if (idx >= size) {
            size = (int)fread(buf, 1, BUFSIZE, stdin);
            idx = 0;
            if (size == 0) return 0;
        }
        return buf[idx++];
    }

    template <class T>
    bool readInt(T &out) {
        char c;
        do {
            c = readChar();
            if (!c) return false;
        } while (c <= ' ');

        bool neg = false;
        if (c == '-') {
            neg = true;
            c = readChar();
        }

        T val = 0;
        while (c > ' ') {
            val = val * 10 + (c - '0');
            c = readChar();
        }
        out = neg ? -val : val;
        return true;
    }
};

struct FastOutput {
    static constexpr size_t BUFSIZE = 1 << 20;
    char buf[BUFSIZE];
    size_t idx = 0;

    ~FastOutput() { flush(); }

    inline void flush() {
        if (idx) {
            fwrite(buf, 1, idx, stdout);
            idx = 0;
        }
    }

    inline void pushChar(char c) {
        if (idx >= BUFSIZE) flush();
        buf[idx++] = c;
    }

    inline void writeInt(long long x) {
        if (x == 0) {
            pushChar('0');
            return;
        }
        if (x < 0) {
            pushChar('-');
            x = -x;
        }
        char s[24];
        int n = 0;
        while (x) {
            s[n++] = char('0' + (x % 10));
            x /= 10;
        }
        while (n--) pushChar(s[n]);
    }

    inline void writeStr(const char* s) {
        while (*s) pushChar(*s++);
    }

    inline void writeNewline() { pushChar('\n'); }
};

struct AdjEdge {
    int to, id;
};

int main() {
    FastScanner fs;
    FastOutput fo;

    int T;
    if (!fs.readInt(T)) return 0;

    while (T--) {
        int n;
        fs.readInt(n);

        vector<int> p(n + 1), pos(n + 1);
        for (int i = 1; i <= n; i++) {
            fs.readInt(p[i]);
            pos[p[i]] = i;
        }

        vector<vector<AdjEdge>> adj(n + 1);
        adj.assign(n + 1, {});
        for (int i = 1; i <= n - 1; i++) {
            int u, v;
            fs.readInt(u);
            fs.readInt(v);
            adj[u].push_back({v, i});
            adj[v].push_back({u, i});
        }

        vector<char> active(n + 1, 1);
        vector<int> deg(n + 1, 0);
        deque<int> dq;
        for (int i = 1; i <= n; i++) {
            deg[i] = (int)adj[i].size();
        }
        for (int i = 1; i <= n; i++) {
            if (deg[i] <= 1) dq.push_back(i);
        }

        vector<int> ops;
        ops.reserve(n * n);

        vector<char> vis(n + 1, 0);
        vector<int> parent(n + 1, 0), parentE(n + 1, 0);
        vector<int> st;
        st.reserve(n);

        auto doSwap = [&](int a, int b, int eid) {
            ops.push_back(eid);
            int pa = p[a], pb = p[b];
            p[a] = pb; p[b] = pa;
            pos[pa] = b;
            pos[pb] = a;
        };

        int activeCount = n;
        while (activeCount > 1) {
            int v = -1;
            while (!dq.empty()) {
                int x = dq.front();
                dq.pop_front();
                if (active[x] && deg[x] == 1) {
                    v = x;
                    break;
                }
            }
            if (v == -1) break; // should not happen

            int start = pos[v];
            if (start != v) {
                fill(vis.begin(), vis.end(), 0);
                st.clear();
                st.push_back(v);
                vis[v] = 1;
                parent[v] = 0;
                parentE[v] = 0;

                while (!st.empty()) {
                    int cur = st.back();
                    st.pop_back();
                    if (cur == start) break;
                    for (const auto &e : adj[cur]) {
                        int to = e.to;
                        if (!active[to] || vis[to]) continue;
                        vis[to] = 1;
                        parent[to] = cur;
                        parentE[to] = e.id;
                        st.push_back(to);
                    }
                }

                int cur = start;
                while (cur != v) {
                    int nxt = parent[cur];
                    int eid = parentE[cur];
                    doSwap(cur, nxt, eid);
                    cur = nxt;
                }
            }

            active[v] = 0;
            activeCount--;
            int neigh = 0;
            for (const auto &e : adj[v]) {
                if (active[e.to]) {
                    neigh = e.to;
                    break;
                }
            }
            if (neigh) {
                deg[neigh]--;
                if (deg[neigh] == 1 && activeCount > 1) dq.push_back(neigh);
            }
            deg[v] = 0;
        }

        fo.writeInt((long long)ops.size());
        fo.writeNewline();
        for (int eid : ops) {
            fo.writeInt(1);
            fo.pushChar(' ');
            fo.writeInt(eid);
            fo.writeNewline();
        }
    }

    fo.flush();
    return 0;
}