#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 NN rooms in the IOI Prison, numbered 1,,N1,\dots,N. There are N1N-1 corridors. The ii-th corridor (1iN1)(1\le i\le N-1) connects rooms AiA_i and BiB_i in both directions. Any two rooms are reachable from each other.

There are MM prisoners in the IOI Prison, numbered 1,,M1,\dots,M. The bedroom and workshop of the jj-th prisoner (1jM)(1\le j\le M) are rooms Sj,TjS_j,T_j, 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 MM 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 QQ, indicating that this testdata contains QQ test cases.

For each test case:

The first line contains an integer NN, the number of rooms.

The next N1N-1 lines each contain two integers Ai,BiA_i,B_i, describing a corridor connecting the two rooms.

The next line contains an integer MM, the number of prisoners.

The next MM lines each contain two integers Si,TiS_i,T_i, the bedroom and workshop of a prisoner.

Output Format

Output QQ lines. On the ii-th line, for the ii-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:

  1. Let prisoner 22 move from room 44 to room 55.
  2. Let prisoner 11 move from room 33 to room 44.
  3. Let prisoner 22 move from room 55 to room 66.
  4. Let prisoner 22 move from room 66 to room 77.
  5. Let prisoner 22 move from room 77 to room 88.

This sample satisfies the constraints of all subtasks.

[Sample Explanation #2]

This sample satisfies the constraints of subtasks 1,3,4,5,6,71,3,4,5,6,7.

[Constraints]

For all data, it holds that:

  • 1Q10001\leq Q\leq 1000.
  • 1N1200001\leq N\leq 120000.
  • 1Ai<BiN1\leq A_i\lt B_i\leq N (i[1,N1])(i\in [1,N-1]).
  • 2MN2\leq M\leq N.
  • 1Si,TiN1\leq S_i,T_i\leq N (i[1,M])(i\in [1,M]).
  • SiS_i (i[1,M])(i\in[1,M]) are pairwise distinct.
  • TiT_i (i[1,M])(i\in[1,M]) are pairwise distinct.
  • SjTjS_j \ne T_j (j[1,M])(j\in [1, M]).
  • Any two rooms are mutually reachable via the given roads.
  • Over all test cases, the sum of NN does not exceed 120000120000.

The additional constraints and scores for each subtask are given in the table below:

Subtask ID Additional Constraints Score
11 Ai=i,Bi=i+1 (i[1,N1])A_i=i,B_i=i+1~(i\in[1,N-1]) 55
22 Q20,N250,M=2Q\leq 20, N\leq 250, M=2
33 Q20,N250,M6Q\leq 20, N\leq 250, M\leq 6 1616
44 Q20,N250,M100Q\leq 20, N\leq 250, M\leq 100 2828
55 Q20,M500Q\leq 20, M\leq 500 1212
66 Any two rooms can be reached via at most 2020 roads. 1111
77 No additional constraints. 2323

Translated by ChatGPT 5