QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100

#23259. Family Trees

Statistics

While exploring the ruins of the ancient Amazon kingdom, Dr. Kim discovered two genealogies of queens. Each genealogy records, as a tree, the parent-child relationships between one original queen and her female descendants, as shown below. For example, in Figure 1, $C$ is the original queen, and $F$ and $D$ are children of $C$. The children of a person are unordered.

problem_23259_311bbb645078eabc59db67925c556b02.png

Figure 1

It was found that, before the two genealogies were discovered, zero or more elementary damage operations, defined below, had occurred.

Elementary damage: A parent $P$ and a child $Q$ of $P$ are merged into one person. All children of both people, except $Q$, become children of the merged person.

For example, applying elementary damage that merges $A$ and $F$ in Figure 2 gives Figure 3. Applying another elementary damage operation merging $B$ and $D$ in Figure 3 (Figure 4) then gives Figure 5.

problem_23259_301fc0f647a0d1cbb0b535e05a61aab8.png

Figure 2

problem_23259_c78ed595c960e0bc9518e52ef6b2842b.png

Figure 3

problem_23259_d5f09670c76351bb08f1ca75e6d29258.png

Figure 4

problem_23259_d57e4c974c2c77a374e0fc9ae6b10b25.png

Figure 5

Applying elementary damage that merges $E$ and $G$ in Figure 5 (Figure 6) gives Figure 7.

problem_23259_10c02c0c78bf1eb310b2926ae9fc3c49.png

Figure 6

problem_23259_12e0c2507b723736ba848eaa343015f2.png

Figure 7

Fortunately, no elementary damage involved a person without children (those with numeric names in the figures). All people without children still have their names recorded, and their names are distinct. However, none of the names of people with children (those with alphabetic names in the figures) remain. Thus, the discovered genealogies look like Figure 8.

problem_23259_cbd63bede0c7fa2828f3422a0ff42eb8.png

Figure 8

Different sequences of elementary damage on the same original genealogy can produce different results. However, the two genealogies in Figure 9 cannot come from the same original.

problem_23259_e1d983c3048847e7bfb10b40efe38f85.png

Figure 9

Write a program that determines whether the two given genealogies could have been obtained from the same original through zero or more elementary damage operations. The sets of names of childless people in the two given genealogies $S$ and $T$ are identical. That is, the set formed from the names of childless people in $S$ equals the corresponding set in $T$.

The recommended source file name is family.c or family.cpp, but a different name may also be submitted to the server.

Input

The following information is given on standard input. The first line contains the number of people $N_1$ in the first genealogy, the number $N_2$ in the second, and the number of childless people $K$ in each. People are numbered from $1$ to $N_1$ in the first genealogy and from $1$ to $N_2$ in the second. In both genealogies, people $1$ through $K$ have no children. People with the same number from $1$ through $K$ have the same name.

The next line gives each person’s parent number in the first genealogy, in order of the person’s number; $0$ is given for a person with no parent. The following line gives each person’s parent number in the second genealogy in the same order; $0$ is again given if there is no parent.

Output

On standard output, print YES if the two genealogies can come from the same original, or NO if this is impossible, using uppercase English letters.

Subtasks

In all subtasks, $1 \le K < N_1,N_2$.

  • 1: 17/100 points; $2 \le N_1,N_2 \le 300$. The first genealogy has undergone no elementary damage.
  • 2: 27/100 points; $2 \le N_1,N_2 \le 300$.
  • 3: 15/100 points; $2 \le N_1,N_2 \le 5\,000$.
  • 4: 41/100 points; $2 \le N_1,N_2 \le 300\,000$.

Examples

Input 1

3 3 2
3 3 0
3 3 0

Output 1

YES

Input 2

5 5 3
4 4 5 5 0
4 5 5 0 4

Output 2

NO

Input 3

7 8 4
7 7 6 6 0 5 5
6 7 7 8 0 5 5 5

Output 3

NO

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.