Social Distancing
Time limit10sMemory limit512 MB
Move k students on a tree onto the k computer rooms one adjacent step at a time, keeping students at least two rooms apart, or report NO.
- Level
Hard8 of 10
- Topics
- Tree, Simulation, Implementation
- Solved
- No attempts yet
Statement
Two things can be said about students. They hate doing more work than necessary, and they love distancing themselves from others. The first is probably why the department forms a tree (building a corridor between two indirectly connected rooms would be a waste of time). The second is why they thrive during the ongoing pandemic. Social distancing is no longer a luxury. It is the norm!
However, tree-structured buildings and distancing from others do not go well together. Currently there are students in some of the rooms, and under the distancing policy each room holds at most one student. Also, no two students are in rooms directly connected by a corridor.
The ICPC competition starts soon, and students rush to take seats at the computers scattered around the department. There are computers, as many as there are students, placed in some of the rooms. No two computers share a room, and no two directly connected rooms both have a computer. Students can assign themselves to computers in any way, but they must keep social distancing at all times. Getting them to their seats can be tricky, if not impossible.
You are a ruthless ICPC organizer and the creator of the ultimate killer problemset. Watching students run around frantically, you realize a horrible truth: if the students do not reach their rooms in time, they cannot take part in the competition, and all the hard work on unsolvable problems goes to waste. You cannot allow this.
Given the current positions of the students and the computers, design a sequence of operations that moves every student to a room with a computer. Each operation moves one student to an adjacent room. After every operation, no two students may be in the same room or in two adjacent rooms. You may perform at most moves, where is the number of rooms. The task may be impossible, but there is only one way to find out.
Input
The first line of input contains the number of test cases (). The descriptions of the test cases follow.
The first line of a test case contains a single integer (), the number of rooms at the department.
The next lines contain two integers , each (), two rooms connected by a corridor. The corridors form a tree (a connected graph without cycles).
The next line contains a single integer (), the number of students (and computers).
The next line contains integers (), the initial locations of the students.
The next line contains integers in the same format, denoting the rooms with computers.
It is guaranteed that at least one student is located in a room without a computer.
The sum of over all test cases does not exceed .
Output
For each test case, output YES if it is possible to move the students to rooms with computers while maintaining social distancing, and NO otherwise. In the former case, print any valid solution on the following lines. The solution starts with a single integer (), the number of moves. Then lines follow, each with two integers , (), meaning that the student currently in room moves to room , which is connected to by a corridor.
You do not need to minimize the length of the solution.