#P9520. [JOIST 2022] 监狱 / Jail
[JOIST 2022] 监狱 / Jail
Background
JOIST 2022 D1T1.
Problem Description
In the JOI Kingdom, the place with the strictest security is the IOI Prison. There are rooms in the IOI Prison, numbered . There are corridors. The -th corridor connects rooms and in both directions. Any two rooms are reachable from each other.
There are prisoners in the IOI Prison, numbered . The bedroom and workshop of the -th prisoner are rooms , respectively. A prisoner may work in another prisoner's bedroom. However, each room can be the bedroom of at most one prisoner, and the workshop of at most one prisoner.
One morning, these prisoners need to move from their bedrooms to their workshops. Warden Mr. APIO must instruct the prisoners to move in the following way:
- Instruction: Choose a prisoner, then order him to move from his current room to a room that is directly connected to it by an edge. To avoid communication between prisoners, it is not allowed to move a prisoner into a room where another prisoner is present.
To start work as early as possible, Mr. APIO wants to know whether there exists a plan consisting of any number of instructions such that every prisoner can reach his workshop from his bedroom along a shortest path.
Write a program that, given all the information about the rooms, corridors, and prisoners as described above, determines whether such a plan exists.
Input Format
Each testdata contains multiple test cases.
The first line contains an integer , indicating that this testdata contains test cases.
For each test case:
The first line contains an integer , the number of rooms.
The next lines each contain two integers , describing a corridor connecting the two rooms.
The next line contains an integer , the number of prisoners.
The next lines each contain two integers , the bedroom and workshop of a prisoner.
Output Format
Output lines. On the -th line, for the -th test case, output Yes if there exists a plan that satisfies the requirements, otherwise output No.
1
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
2
3 4
4 8
Yes
2
7
1 2
2 3
3 4
4 5
3 6
6 7
2
4 1
5 7
4
1 2
1 3
1 4
3
2 3
3 4
4 2
Yes
No
Hint
[Sample Explanation #1]
The task can be completed by issuing the following instructions:
- Let prisoner move from room to room .
- Let prisoner move from room to room .
- Let prisoner move from room to room .
- Let prisoner move from room to room .
- Let prisoner move from room to room .
This sample satisfies the constraints of all subtasks.
[Sample Explanation #2]
This sample satisfies the constraints of subtasks .
[Constraints]
For all data, it holds that:
- .
- .
- .
- .
- .
- are pairwise distinct.
- are pairwise distinct.
- .
- Any two rooms are mutually reachable via the given roads.
- Over all test cases, the sum of does not exceed .
The additional constraints and scores for each subtask are given in the table below:
| Subtask ID | Additional Constraints | Score |
|---|---|---|
| Any two rooms can be reached via at most roads. | ||
| No additional constraints. |
Translated by ChatGPT 5