luogu#P8499. [NOI2022] 挑战 NPC Ⅱ

[NOI2022] 挑战 NPC Ⅱ

Problem Description

Zhu Youyang is a laid-back college student, but he still dreams every day of solving NPC problems in polynomial time.

In class, Zhu Youyang learned that the subgraph isomorphism problem is an NPC problem. He plans to give a polynomial-time decision algorithm for subgraph isomorphism, and indirectly prove that P=NPP = NP. Then he will surely win the Turing Award with this great work! Unfortunately, Zhu Youyang is not knowledgeable enough, and he cannot even come up with a proof that subgraph isomorphism is NPC. Therefore, he steps back and decides to solve a simpler problem:

Given two rooted trees G,HG, H. Let G\lvert G \rvert denote the number of nodes in tree GG. The two trees satisfy the following constraint: $1 \leq \lvert H \rvert \leq \lvert G \rvert \leq \lvert H \rvert + k$. Here Zhu Youyang guarantees that kk is a small constant.

Zhu Youyang may delete some nodes in GG, and let the subgraph obtained after deletion be GG'. He wants to know whether there exists a way to delete nodes such that the resulting subgraph GG' satisfies:

  • GG' is connected.
  • GG' contains the root of GG (that is, the root of GG is not deleted during the deletion process).
  • GG' is isomorphic to HH (that is, there exists a way to relabel the nodes of GG' such that the relabeled graph is exactly the same as HH, and the root of GG becomes exactly the root of HH after relabeling).

Input Format

This problem contains multiple test cases.

The first line of input contains two positive integers C,TC, T and a non-negative integer kk. These three numbers represent the current subtask index, the number of test cases, and the constant given in the statement. If the current testdata is a sample, then C=0C = 0. It is guaranteed that T500T \leq 500 and k5k \leq 5.

For each test case:

The first line contains a positive integer n1n_1, the number of nodes in tree GG. It is guaranteed that 1n11051 \leq n_1 \leq {10}^5, and n15×105\sum n_1 \leq 5 \times {10}^5.

The second line contains n1n_1 integers describing the structure of tree GG. Specifically, the ii-th integer (1in11 \leq i \leq n_1) aia_i indicates the parent of node ii in tree GG. If it is the root, then ai=1a_i = -1. It is guaranteed that the tree obtained by the above rule is a connected rooted tree.

The third line contains a positive integer n2n_2, the number of nodes in HH. It is guaranteed that for all testdata, max(1,n1k)n2n1\max(1, n_1 - k) \leq n_2 \leq n_1.

The fourth line contains n2n_2 integers describing the structure of tree HH. Specifically, the ii-th integer (1in21 \leq i \leq n_2) bib_i indicates the parent of node ii in tree HH. If it is the root, then bi=1b_i = -1. It is guaranteed that the tree obtained by the above rule is a connected rooted tree.

Output Format

For each test case:

Output one line containing a string. If there exists a way to delete nodes from GG such that all three conditions above are satisfied, output Yes; otherwise output No.

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

Yes
No
Yes

Hint

Sample Explanation #1

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

For the second test point, the depth of the first input tree is 11, but the depth of the second input tree is 22. Therefore, no matter how we delete nodes from the first tree, its height cannot increase to 22, so the output is No.

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


Sample #2

See iso/iso2.in and iso/iso2.ans in the attachments.

The constraints of this sample satisfy test points 787 \sim 8.


Sample #3

See iso/iso3.in and iso/iso3.ans in the attachments.

The constraints of this sample satisfy test points 9109 \sim 10.


Sample #4

See iso/iso4.in and iso/iso4.ans in the attachments.

The constraints of this sample satisfy test point 1313.


Constraints

For all testdata, it holds that 1T5001 \leq T \leq 500, 1n2n11051 \le n_2 \leq n_1 \le {10}^5, n15×105\sum n_1 \leq 5 \times {10}^5, 0k50 \leq k \leq 5. The additional constraints for each test point are shown in the table below:

n1,n2n_1,n_2 n1\sum n_1 Test Points kk Special Property
8\leq 8 500\leq 500 131 \sim 3 0\leq 0 None
464 \sim 6 5\leq 5
16\leq 16 103\leq 10^3 787 \sim 8 0\leq 0
9109 \sim 10 5\leq 5
150\leq 150 104\leq 10^4 1111 0\leq 0
1212 1\leq 1
1313 5\leq 5
105\leq 10^5 5×105\leq 5 \times 10^5 141614 \sim 16 0\leq 0 A
172017 \sim 20 B
2121 1\leq 1 None
222322 \sim 23 3\leq 3
242524 \sim 25 5\leq 5

The special properties in the additional constraints are as follows:

  • Special Property A: It is guaranteed that in rooted tree GG, every node is either a leaf or has exactly 11 child. An equivalent statement is that rooted tree GG forms a chain, and the root is one endpoint of the chain.
  • Special Property B: It is guaranteed that in rooted tree GG, every node is either a leaf or has exactly 22 children, and all leaves of GG have the same depth. An equivalent statement is that rooted tree GG is a perfect binary tree, and the root is the root of that perfect binary tree.

Hint

The testdata has not been constructed to target any reasonable hashing algorithm, so within a reasonable range there is no need to worry too much about losing points due to hash collisions.

Translated by ChatGPT 5