luogu#P16613. [Algo Beat 008 & WWOI R3] Tree Reconfiguration

[Algo Beat 008 & WWOI R3] Tree Reconfiguration

Background

Please note that if your program gets MLE, TLE, OLE, or RE, you will not obtain 40% of the score even if your Yes/No judgement is correct.

Problem Description

Murasame-chan gives you a tree with nn vertices, two sets SS and TT of size kk, and an integer DD. It is guaranteed that all elements in SS are distinct, and all elements in TT are distinct.

Initially, all vertices ii such that iSi \in S are marked, and all other vertices are unmarked.

You may perform the following operation any number of times, possibly zero, until all vertices ii such that iTi \in T are marked:

  • Choose a currently marked vertex uu and a currently unmarked vertex vv adjacent to uu. The distance from vv to every currently marked vertex must be at most DD.
  • Then unmark uu and mark vv.

Murasame-chan asks whether there exists a valid sequence of operations to achieve the goal. Since she is very cute, if such a sequence exists, you need to output it.

You must ensure that the length of the operation sequence does not exceed 106\bm{10^6}.

It can be proven that under the constraints of this problem, if a valid sequence of operations exists, then there is always one of length at most 10610^6.

Specifically, if your Yes/No judgement is correct but your constructed sequence is invalid, you can still obtain 40% of the score for the corresponding test case. Please note that your output must be correctly formatted to obtain these points. If you only want to obtain these points, then when you determine that a solution exists, you may output a correctly formatted but not necessarily valid sequence, for example, c=0c=0.

Input Format

The first line contains three integers nn, kk, and DD, denoting the number of vertices in the tree, the size of the sets, and the distance limit, respectively.

Each of the next n1n-1 lines contains two integers uu and vv (1u,vn1 \leq u, v \leq n), denoting an edge between vertices uu and vv.

The next line contains kk distinct integers, denoting the set SS.

The next line contains kk distinct integers, denoting the set TT.

It is guaranteed that the maximum distance between any two vertices in S\bm{S} is at most D\bm{D}, and the maximum distance between any two vertices in T\bm{T} is at most D\bm{D}.

Output Format

If a solution exists:

  • Print Yes on the first line.
  • On the second line, print an integer cc, denoting the length of your operation sequence. You must ensure that 0c1060 \leq c \leq 10^6.
  • Then print cc lines. Each line should contain two integers uu and vv, denoting an operation: unmark uu and mark vv. You must ensure that every operation is valid.

Any operation sequence satisfying the conditions will be accepted.

If no solution exists:

  • Print a single string No on one line.

The special judge is case-insensitive, that is, you may print Yes and No in any case, such as yes, YES, yEs, NO, or nO.

5 2 2
1 2
2 3
3 4
4 5
1 2
4 5
Yes
6
2 3
1 2
3 4
2 3
4 5
3 4
7 3 3
1 2
2 3
3 4
3 5
5 6
5 7
1 2 3
5 6 7
Yes
8
3 5
2 3
1 2
5 6
3 5
2 3
5 7
3 5
4 2 1
1 2
2 3
3 4
1 2
3 4
No

Hint

Explanation for Sample 2

See the image below. The orange vertices represent the marked vertices that move during the operation, the blue vertices represent the marked vertices that do not move during the operation, and the arrows represent the direction of movement.

:::info[Image] :::

Note: The outputs for the first two samples are not unique.

Constraints

This problem uses bundled testing.

For all test cases, it is guaranteed that 1n20001 \leq n \leq 2000, 2kn2 \leq k \leq n, and 1Dn1 \leq D \leq n.

::cute-table{tuack} | Subtask | Additional Constraints | Score | | :-: | :-: | :-: | | 1 | S=TS=T | 5 | | 2 | D=1D=1 | 5 | | 3 | n12n \leq 12 | 10 | | 4 | The tree is a single chain | 10 | | 5 | k=2k=2 | 15 | | 6 | DD \geq diameter of the tree | 15 | | 7 | No additional constraints | 40 |

Specifically, Subtask 7 depends on all previous subtasks.