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 . 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 . Let denote the number of nodes in tree . 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 is a small constant.
Zhu Youyang may delete some nodes in , and let the subgraph obtained after deletion be . He wants to know whether there exists a way to delete nodes such that the resulting subgraph satisfies:
- is connected.
- contains the root of (that is, the root of is not deleted during the deletion process).
- is isomorphic to (that is, there exists a way to relabel the nodes of such that the relabeled graph is exactly the same as , and the root of becomes exactly the root of after relabeling).
Input Format
This problem contains multiple test cases.
The first line of input contains two positive integers and a non-negative integer . 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 . It is guaranteed that and .
For each test case:
The first line contains a positive integer , the number of nodes in tree . It is guaranteed that , and .
The second line contains integers describing the structure of tree . Specifically, the -th integer () indicates the parent of node in tree . If it is the root, then . It is guaranteed that the tree obtained by the above rule is a connected rooted tree.
The third line contains a positive integer , the number of nodes in . It is guaranteed that for all testdata, .
The fourth line contains integers describing the structure of tree . Specifically, the -th integer () indicates the parent of node in tree . If it is the root, then . 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 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 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 , but the depth of the second input tree is . Therefore, no matter how we delete nodes from the first tree, its height cannot increase to , 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 .
Sample #3
See iso/iso3.in and iso/iso3.ans in the attachments.
The constraints of this sample satisfy test points .
Sample #4
See iso/iso4.in and iso/iso4.ans in the attachments.
The constraints of this sample satisfy test point .
Constraints
For all testdata, it holds that , , , . The additional constraints for each test point are shown in the table below:
| Test Points | Special Property | |||
|---|---|---|---|---|
| None | ||||
| A | ||||
| B | ||||
| None | ||||
The special properties in the additional constraints are as follows:
- Special Property A: It is guaranteed that in rooted tree , every node is either a leaf or has exactly child. An equivalent statement is that rooted tree forms a chain, and the root is one endpoint of the chain.
- Special Property B: It is guaranteed that in rooted tree , every node is either a leaf or has exactly children, and all leaves of have the same depth. An equivalent statement is that rooted tree 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