JOI '22 Spring Camp Day 1 P1 - Jail

View as PDF

Submit solution


Points: 30 (partial)
Time limit: 4.0s
Memory limit: 1G

Problem type

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 \le i \le 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 an "ID number", which is an integer between 1 and M, inclusive. The bedroom of the prisoner j (1 \le j \le 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 bedrooms to their workrooms. 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.

Input Specification

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 number of rooms N in IOI Jail.
  • Information on the passages in IOI Jail (A_1, B_1), (A_2, B_2), \dots, (A_{N-1}, B_{N-1}).
  • The number of prisoners M in IOI Jail.
  • Information on the bedrooms and the workrooms of the prisoners (S_1, T_1), (S_2, T_2), \dots, (S_M, T_M).

The format of the input data is as follows. Given values are all integers

Q

(Input for Scenario 1)

(Input for Scenario 2)

\vdots

(Input for Scenario Q)

The format of the input data for each scenario is as follows. See Sample Input and Output for details.

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

Output Specification

Write Q lines to the standard output. The k-th line (1 \le k \le 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.

Constraints

  • 1 \le Q \le 1\,000.
  • 2 \le N \le 120\,000.
  • 1 \le A_i < B_i \le N (1 \le i \le N-1).
  • 2 \le M \le N.
  • 1 \le S_j \le N (1 \le j \le M).
  • 1 \le T_j \le N (1 \le j \le M).
  • S_1, S_2, \dots, S_M are different from each other.
  • T_1, T_2, \dots, T_M are different from each other.
  • S_j \neq T_j (1 \le j \le M).
  • It is possible to move from any room to any other room by passing through several passages.
  • The sum of N for the Q scenarios is less than or equal to 120\,000.

Subtasks

  1. (5 points) A_i = i, B_i = i+1 (1 \le i \le N-1).
  2. (5 points) Q \le 20, N \le 250, M = 2.
  3. (16 points) Q \le 20, N \le 250, M \le 6.
  4. (28 points) Q \le 20, N \le 250, M \le 100.
  5. (12 points) Q \le 20, M \le 500.
  6. (11 points) It is possible to move from any room to any other room by passing through at most 20 passages.
  7. (23 points) No additional constraints.

Sample Input 1

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

Sample Output 1

Yes

Explanation for Sample 1

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.

  1. Move the prisoner 2 from the room 4 to the room 5.
  2. Move the prisoner 1 from the room 3 to the room 4.
  3. Move the prisoner 2 from the room 5 to the room 6.
  4. Move the prisoner 2 from the room 6 to the room 7.
  5. Move the prisoner 2 from the room 7 to the room 8.

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.

Sample Input 2

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

Sample Output 2

Yes
No

Explanation for Sample 2

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.

  1. Move the prisoner 1 from the room 4 to the room 3.
  2. Move the prisoner 1 from the room 3 to the room 2.
  3. Move the prisoner 2 from the room 5 to the room 4.
  4. Move the prisoner 2 from the room 4 to the room 3.
  5. Move the prisoner 2 from the room 3 to the room 6.
  6. Move the prisoner 1 from the room 2 to the room 1.
  7. Move the prisoner 2 from the room 6 to the room 7.

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.

Sample Input 3

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

Sample Output 3

Yes
No
Yes

Explanation for Sample 3

This sample input satisfies the constraints of Subtasks 1, 3, 4, 5, 6, 7.


Comments

There are no comments at the moment.