시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
4 초 | 1024 MB | 102 | 23 | 18 | 22.785% |
In JOI Kingdom, the security of IOI Jail is strictly controlled. There are $N$ rooms in IOI Jail, numbered from $1$ to $N$. There are $N - 1$ passages connecting rooms. The passage $i$ ($1 ≤ i ≤ N - 1$) connects the room $A_i$ and the room $B_i$ bidirectionally. It is possible to move from any room to any other room by passing through several passages.
There are $M$ prisoners in IOI Jail. Each prisoner has a “ID number”, which is an integer between $1$ and $M$, inclusive. The bedroom of the prisoner $j$ ($1 ≤ j ≤ M$) is the room $S_j$, and the workroom of the prisoner $j$ is the room $T_j$. A prisoner may work in the bedroom of another prisoner. However, no two prisoners share the same bedroom, and no two prisoners share the same workroom.
One morning, the $M$ prisoners have to move from their bed rooms to their work rooms. Mr. APIO is the director of IOI Jail. He will give the following directions to the prisoners to move.
Direction Choose a prisoner, and move the chosen prisoner from the current room to another room which is connected with the current room by a passage. In order to avoid communication between prisoners, it is not allowed to move the prisoner to a room where another prisoner stays.
In order to start work as early as possible, Mr. APIO wants to know whether it is possible to give directions so that every prisoner does not pass through the same room more than once (it means that every prisoner takes a shortest path).
Write a program which, given information of the rooms and the passages in IOI Jail and information on the prisoners, determines whether it is possible to move the prisoners so that every prisoner takes a shortest path.
A test case consists of Q scenarios, numbered from 1 to Q. The following values are specified for each scenario. For the range of these values, see Constraints.
The format of the input data is as follows. Given values are all integers.
$Q$
(Input for Scenario $1$)
(Input for Scenario $2$)
. . .
(Input for Scenario $Q$)
The format of the input data for each scenario is as follows. See Sample Input and Output for details.
$\begin{align*}& N \\ & A_1\,B_1 \\ & A_2 \, B_2 \, \\ & \vdots \\ & A_{N-1} \, B_{N-1} \\ & M \\ & S_1 \, T_1 \\ & S_2 \, T_2 \\ & \vdots \\ & S_M \, T_M\end{align*}$
Write $Q$ lines to the standard output.
The $k$-th line ($1 ≤ k ≤ Q$) should contain the following.
Yes
if it is possible to move the prisoners for the scenario $k$.No
if it is not possible to move the prisoners for the scenario $k$.번호 | 배점 | 제한 |
---|---|---|
1 | 5 | $A_i = i$, $Bi = i + 1$ ($1 ≤ i ≤ N - 1$). |
2 | 5 | $Q ≤ 20$, $N ≤ 250$, $M = 2$. |
3 | 16 | $Q ≤ 20$, $N ≤ 250$, $M ≤ 6$. |
4 | 28 | $Q ≤ 20$, $N ≤ 250$, $M ≤ 100$. |
5 | 12 | $Q ≤ 20$, $M ≤ 500$. |
6 | 11 | It is possible to move from any room to any other room by passing through at most $20$ passages. |
7 | 23 | No additional constraints. |
1 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 2 3 4 4 8
Yes
For this sample input, if the director gives the following directions, it is possible to move the prisoners so that every prisoner takes a shortest path.
Therefore, output Yes
. The structure of the jail for this sample input is as follows.
This sample input satisfies the constraints of all the subtasks.
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
There are $Q = 2$ scenarios. For Scenario $1$, if the director gives the following directions, it is possible to move the prisoners so that every prisoner takes a shortest path.
However, for Scenario $2$, it is not possible to move the prisoners so that every prisoner takes a shortest path. Therefore, the first line of output should be Yes
, and the second line of output should be No
.
This sample input satisfies the constraints of Subtasks 3, 4, 5, 6, 7.
3 3 1 2 2 3 2 2 1 3 2 7 1 2 2 3 3 4 4 5 5 6 6 7 3 1 3 4 2 2 5 8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 4 1 5 2 6 3 7 4 8
Yes No Yes
This sample input satisfies the constraints of Subtasks 1, 3, 4, 5, 6, 7.