NOI '22 P4 - Challenge NPC

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 2.0s
Memory limit: 1G

Problem type

Given two rooted trees G, H. Let |G| represent the number of nodes in tree G, then the two trees satisfy the following constraints: 1|H||G||H|+k. It guarantees that k is a small constant. You can delete several nodes in G, assuming that the subgraph obtained after deleting the nodes is G. He wants to know if there is a way to delete nodes such that the subgraph G obtained after deletion satisfies the following conditions:

  • G is connected.
  • G contains the root node in G (that is, the G root node is not deleted during deletion).
  • G and H are isomorphic (that is, there is a way to relabel the points in G, so that the graph obtained by relabeling is exactly the same as H, and the root node in G is exactly the root of H after relabeling the nodes).

Input format

There are multiple sets of test data for this question. The first line of input contains two positive integers C, T and a non-negative integer k. The three numbers represent the current test point number, the number of test data groups and the constant given in the question. C=0 if the current test data is a sample. It is guaranteed that T500, k5.

For each set of test data: The first line of input contains a positive integer n1, representing the number of nodes in tree G, guaranteeing 1n1105, and n15×105. The second line of input contains n1 integers describing the structure of the tree G. Specifically, the i (1in1)-th integer ai represents the parent node of node i in tree G, and if it is the root node, ai=1. It is guaranteed that the tree obtained according to the above rules is a connected rooted tree. The third line of input contains a positive integer n2, representing the number of nodes in H, which is guaranteed to satisfy max(1,n1k)n2n1 for all test data. The fourth line of input contains n2 integers describing the structure of the tree H. Specifically, the i (1in2) integer bi represents the parent node of node i in tree H, and if it is the root node, bi=1. It is guaranteed that the tree obtained according to the above rules is a connected rooted tree.

Output format

For each set of test data: Output one string per line. If there is a way to delete the node in G so that it can satisfy the above three conditions at the same time, output Yes; otherwise, output No.

Samples

Sample inputs and outputs can be found here.

Sample Input 1

Copy
0 3 1
3
2 -1 2
2
-1 1
4
3 3 -1 3
3
2 3 -1
5
-1 1 5 5 1
5
2 3 -1 3 2

Sample Output 1

Copy
Yes
No
Yes

Explanation of Sample 1

For the first test point, we delete node 1 of the first tree. At this point the remaining tree and the input second tree are both rooted trees with two nodes, so the output is Yes.

For the second test point, enter a depth of 1 for the first tree, but a depth of 2 for the second tree. Therefore, deleting the node of the first tree will not cause its tree height to increase to 2, so the output is No.

For the third test point, the input two trees are isomorphic to the tree in the figure below, so the output is Yes.

Sample 2

See iso/iso2.in and iso/iso2.ans in the player directory. The sample data range satisfies test points 7 ∼ 8

Sample 3

See iso/iso3.in and iso/iso3.ans in the player directory. The sample data range satisfies test points 9 ∼ 10.

Sample 4

See iso/iso4.in and iso/iso4.ans in the player directory. The sample data range meets test point 13.

Subtasks

For all test data, 1T500, 1n2n1105, n15×105, 0k5. Additional restrictions for each test point are shown in the table below:

n1, n2n1CasekSpecial Property
85001,2,30None
4, 5, 65
161037, 80
9, 105
150104110
121
135
1055×10514, 15, 160A
17, 18, 19, 20B
211None
22, 233
24, 255

The special properties among the additional restrictions are as follows:

  • Special property A: It is guaranteed that each node of the rooted tree G is either a leaf node or has exactly 1 child node; another equivalent expression is that the rooted tree G constitutes a chain, and the root node is a chain an endpoint.
  • Special property B: It is guaranteed that each node of the rooted tree G is either a leaf node or has exactly 2 child nodes, and each leaf node of G is guaranteed to have the same depth; another equivalent expression is a rooted tree G constitutes a complete binary tree, and the root node is the root node of the complete binary tree

Hint

The data does not have any targeted structure for any reasonable hash algorithm, so within a reasonable range, there is no need to worry too much about the loss of points due to hash collisions.


Comments

There are no comments at the moment.