QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 1024 MB Puntuación total: 100 Dificultad: [mostrar] Hackeable ✓

#4660. Challenge NPC Ⅱ

Estadísticas

Zhuyouyang is a college student who, despite being a slacker, still dreams of solving NPC problems in polynomial time.

During class, Zhuyouyang learned that the subgraph isomorphism problem is an NPC problem. He plans to propose a polynomial-time decision algorithm for the subgraph isomorphism problem to indirectly prove $P = NP$, which would surely earn him a Turing Award for this great work! Unfortunately, Zhuyouyang is not talented enough and could not even come up with a proof that the subgraph isomorphism problem belongs to NPC. Therefore, he settled for a simpler problem:

Given two rooted trees $G$ and $H$. Let $|G|$ denote the number of nodes in tree $G$. These two trees satisfy the following constraint: $1 \le |H| \le |G| \le |H| + k$. Here, Zhuyouyang guarantees that $k$ is a small constant.

Zhuyouyang can delete some nodes from $G$. Let $G'$ be the subgraph obtained after deleting these nodes. He wants to know if there exists a way to delete nodes such that the resulting subgraph $G'$ satisfies the following conditions: $G'$ is connected. $G'$ contains the root node of $G$ (meaning the root node of $G$ was not deleted during the process). * $G'$ is isomorphic to $H$ (meaning there exists a way to relabel the nodes in $G'$ such that the resulting graph is identical to $H$, and the root node of $G$ is relabeled to exactly the root node of $H$).

Input

The input is read from the file iso.in. There are multiple test cases in this problem. The first line of the input contains two positive integers $C, T$ and a non-negative integer $k$. These three numbers represent the current test case ID, the number of test cases, and the constant $k$ given in the problem, respectively. If the current test case is a sample, $C = 0$. It is guaranteed that $T \le 500$ and $k \le 5$.

For each test case: The first line contains a positive integer $n_1$, representing the number of nodes in tree $G$. It is guaranteed that $1 \le n_1 \le 10^5$ and $\sum n_1 \le 5 \times 10^5$. The second line contains $n_1$ integers describing the structure of tree $G$. Specifically, the $i$-th integer $a_i$ ($1 \le i \le n_1$) represents the parent of node $i$ in tree $G$. If it is the root node, $a_i = -1$. It is guaranteed that the tree obtained according to these rules is a connected rooted tree. The third line contains a positive integer $n_2$, representing the number of nodes in $H$. It is guaranteed that for all test cases, $\max(1, n_1 - k) \le n_2 \le n_1$. The fourth line contains $n_2$ integers describing the structure of tree $H$. Specifically, the $i$-th integer $b_i$ ($1 \le i \le n_2$) represents the parent of node $i$ in tree $H$. If it is the root node, $b_i = -1$. It is guaranteed that the tree obtained according to these rules is a connected rooted tree.

Output

Output to the file iso.out. For each test case: Output a single string on one line. If there exists a way to delete nodes in $G$ such that the three conditions above are satisfied, output Yes; otherwise, output No.

Examples

Input 1

1 0 3 1
2 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

Output 1

Yes
No
Yes

Note 1

For the first test case, we delete node 1 of the first tree. The remaining tree and the second input tree are both rooted trees containing two nodes, so the output is Yes.

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

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

Input 2

7 0 3 0
...

Output 2

...

Note 2

See iso/iso2.in and iso/iso2.ans in the contestant's directory. This sample data range satisfies test cases 7–8.

Input 3

9 0 3 5
...

Output 3

...

Note 3

See iso/iso3.in and iso/iso3.ans in the contestant's directory. This sample data range satisfies test cases 9–10.

Input 4

13 0 3 5
...

Output 4

...

Note 4

See iso/iso4.in and iso/iso4.ans in the contestant's directory. This sample data range satisfies test case 13.

Subtasks

For all test cases, $1 \le T \le 500$, $1 \le n_2 \le n_1 \le 10^5$, $\sum n_1 \le 5 \times 10^5$, and $0 \le k \le 5$. The additional constraints for each test case are shown in the table below:

$n_1, n_2$ $\sum n_1$ Test Cases $k$ Special Properties
$\le 8$ $\le 500$ 1, 2, 3 $\le 0$ None
4, 5, 6 $\le 5$
$\le 16$ $\le 10^3$ 7, 8 $\le 0$ None
9, 10 $\le 5$
$\le 150$ $\le 10^4$ 11 $\le 0$ None
12 $\le 1$
13 $\le 5$
$\le 10^5$ $\le 5 \times 10^5$ 14, 15, 16 $\le 0$ A
17, 18, 19, 20 $\le 0$ B
21 $\le 1$ None
22, 23 $\le 3$ None
24, 25 $\le 5$ None

The special properties in the additional constraints are as follows:

  • Special Property A: It is guaranteed that every node in the rooted tree $G$ is either a leaf node or has exactly 1 child node; another equivalent statement is that the rooted tree $G$ forms a chain, and the root node is one end of the chain.
  • Special Property B: It is guaranteed that every node in the rooted tree $G$ is either a leaf node or has exactly 2 child nodes, and it is also guaranteed that every leaf node in $G$ has the same depth; another equivalent statement is that the rooted tree $G$ forms a perfect binary tree, and the root node is the root of the perfect binary tree.

Note

The data is not constructed to target any reasonable hashing algorithm, so there is no need to worry excessively about losing points due to hash collisions within a reasonable range.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.