Cat Network
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk and Marichka built an incredible system of boxes and tunnels for their \(k\) cats (\(k\) is an even number). In total, there are \(n\) boxes in the network, connected by \(n-1\) bidirectional tunnels such that there is a path between any pair of boxes. In other words, the network is represented as a tree with \(n\) vertices, numbered with integers from 1 to \(n\) inclusive.
It is known that the \(i\)-th cat is based in the box with number \(p_i\). No box contains more than one cat initially. Zenyk and Marichka want to divide the cats into pairs in such a way that there do not exist two such pairs that share a common vertex on the paths between their bases.
Your task is to find a possible pairing.
Input
The first line contains two integers \(n\) and \(k\) — the number of boxes and cats.
The second line contains \(k\) space-separated integers — the bases of the cats \(p_i\).
The next \(n-1\) lines describe the tunnels as pairs of box indices connected by the corresponding tunnel.
Output
If it is impossible to obtain the desired pairing, output
NO.
Otherwise, in the first line output YES.
In the next \(\frac{k}{2}\) lines, output the pairing, one pair per line — the indices of the corresponding boxes in the pair.
If there are multiple valid pairings, you are allowed to output any of them.
Constraints
\(2 \le k \le n \le 10^5\),
\(k\) is an even number.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 4 1 2 3 4 1 2 2 3 2 4 | NO |
| Input (stdin) | Output (stdout) |
|---|---|
| 5 4 5 4 1 3 1 2 2 5 2 3 3 4 | YES 1 5 3 4 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|