Managing Cluster

시간 제한3초메모리 제한2048 MB

요약
2n개 트리 정점 위에 n개 서비스가 각각 두 번 나타날 때, 각 정점이 최대 한 번만 교환에 참여하도록 교환을 선택해 두 복제본이 인접한 정점에 놓이는 서비스 수를 최대로 만든다.
난이도

어려움10점 중 8점

유형
그래프, 동적 계획법, 그리디, 트리
정답자
아직 제출이 없습니다

문제

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 2n−12n-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 a_1,a_2,…,a_2na\_1, a\_2, \ldots, a\_{2n}, where a_ia\_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 a_ia\_i and a_ja\_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 (1≤T≤1051 \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 (1≤n≤1051 \leq n \leq 10^5).

The second line contains 2n2n integers a_1,a_2,…,a_2na\_1, a\_2, \ldots, a\_{2n} (1≤a_i≤n1 \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 2n−12n-1 lines contains two integers uu and vv (1≤u,v≤2n1 \leq u, v \leq 2n, u≠vu \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 (0≤k≤n0 \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 (1≤i,j≤2n1 \leq i, j \leq 2n, i≠ji \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.

힌트

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 1−51-5, 8−38-3, and 4−74-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.

예제1

  1. 예제 1

    입력
    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