luogu#P12104. [NERC2024] Managing Cluster

[NERC2024] Managing Cluster

题目描述

You want to write a cluster manager extension that will improve your product performance. Your product has nn services (numbered from 11 to nn) and is hosted on a cluster with 2n2n machines (numbered from 11 to 2n2n). Each service is running in exactly two replicas. Each replica is run on some machine. Each machine runs exactly one replica of some service.

One of the key factors of this cluster's performance is the network. Some pairs of machines are connected directly and can transfer data between them very efficiently. There are exactly 2n12n-1 direct connections, and it is possible to transfer data between any two machines using direct connections. In other words, direct connections form a tree.

During the deployment, all 2n2n replicas were assigned to machines. Your extension gets the direct connections list and the sequence a1,a2,,a2na_1, a_2, \ldots, a_{2n}, where aia_i is the number of the service that will be running on machine ii. Your extension can swap some replicas between machines. The swap operation takes two machines ii, jj and swaps values aia_i and aja_j. Each machine is allowed to participate in at most one swap operation. Your extension should make some swap operations that maximize the cluster performance.

Due to the fact that most data will be transferred between two replicas of the same service, the cluster performance is measured as the number of services that have two replicas running on machines connected directly. Help to write the extension that will maximize the cluster performance.

输入格式

The first line contains a single integer TT (1T1051 \leq T \leq 10^5) --- the number of test cases. Descriptions of test cases follow.

The first line of each test case contains a single integer nn (1n1051 \leq n \leq 10^5).

The second line contains 2n2n integers a1,a2,,a2na_1, a_2, \ldots, a_{2n} (1ain1 \leq a_i \leq n). It is guaranteed that each value from 11 to nn appears exactly twice in this sequence.

Each of the next 2n12n-1 lines contains two integers uu and vv (1u,v2n1 \leq u, v \leq 2n, uvu \neq v), meaning that machines uu and vv are connected directly. Direct connections are guaranteed to form a tree.

It is guaranteed that the sum of nn for all test cases does not exceed 10510^5.

输出格式

For each test case on the first line print a single integer kk (0kn0 \leq k \leq n) --- the number of swap operations the extension wants to make.

Each of the next kk lines should contain two integers ii, jj (1i,j2n1 \leq i, j \leq 2n, iji \neq j) --- swap operations. Each number from 11 to 2n2n should appear at most once.

Note that the order of operations is not important. After applying swap operations, the cluster performance should be the maximum possible. You can print any answer that satisfies the requirements.

3
2
1 2 2 1
1 2
2 3
3 4
4
4 3 1 3 2 4 1 2
1 2
3 1
3 4
5 1
5 6
2 7
2 8
3
1 1 2 2 3 3
1 2
1 3
1 4
1 5
1 6
1
1 3
3
1 5
8 3
4 7
0

提示

In the first test case only replicas of service 2 run on directly connected machines, so the performance is 1. The performance can be increased to 2 by swapping replicas between machines 1 and 3.

In the second test case no two replicas run on directly connected machines, so the performance is zero. The performance can be increased to 3 by performing swaps 151-5, 838-3, and 474-7 so that replicas of services 2, 3, and 4 run on directly connected machines. It can be shown that it is impossible to get performance 4 here.

In the third test case only replicas of service 1 run on directly connected machines, so the performance is 1. It is obvious that here the performance cannot be made any bigger.