Under construction

Still being built. Things may change

Genealogy

Time limit2sMemory limit512 MB

Summary
Decide whether two queen family trees with the same childless names can both be produced by merging parent-child pairs from one original tree.
Level

Hard8 of 10

Topics
Tree, DFS, Implementation
Solved
No attempts yet

Problem

Dr. Kim was exploring the ruins of an ancient Amazon kingdom when he found two family trees of queens. Each tree records, in tree form, one first queen and the parent-child relationships among her female descendants. For example, in Figure 1, C is the first queen, and F and D are children of C. The order of a person's children does not matter.

Figure 1

When the two family trees were found, it became known that unit corruption had occurred zero or more times. Unit corruption is defined as follows.

Unit corruption: a parent P and a child Q of P merge into one person. All children of the two people (except Q) become children of the merged person.

For example, in Figure 2, if A and F merge by unit corruption, the tree changes as in Figure 3. If B and D then merge (Figure 4), the tree changes as in Figure 5.

Figure 2Figure 3Figure 4Figure 5

If E and G merge in Figure 5 (Figure 6), the tree changes as in Figure 7.

Figure 6Figure 7

Fortunately, no unit corruption involved a childless person (the people with numeric names in the figures). Every childless person's name is written in the tree, and all those names are different. However, none of the names of people with children (the people with letter names in the figures) remain. So the discovered family trees have the shape shown in Figure 8.

Figure 8

Different sequences of unit corruption on the same original can give different results. However, the two family trees in Figure 9 cannot come from the same original.

Figure 9

Write a program that decides whether the two family trees given as input can be produced from the same original through zero or more unit corruptions. The two input trees S and T have the same set of names of childless people. That is, if you collect the names of childless people in S into a set, and do the same for T, the two sets are equal.

Input

Standard input gives the following. The first line contains the number of people in the first tree N1N_1, the number of people in the second tree N2N_2, and the number of childless people in each tree KK. People in the first tree are numbered 1 to N1N_1, and people in the second tree are numbered 1 to N2N_2. In both trees, people 1 through KK have no children. Also, the people numbered 1 through KK have the same name in both trees when their numbers match.

The next line gives the parent number of each person in the first tree, in order of person number. Use 0 if a person has no parent. The line after that gives the parent numbers for the second tree in the same way.

Output

Print YES if the two family trees can be produced from the same original, and NO otherwise. Print the answer in uppercase English letters.

Constraints

In every subtask, 1≤K<N11 \le K < N_1 and K<N2K < N_2.

Examples3

  1. Example 1

    Input
    3 3 2
    3 3 0
    3 3 0
    
    Expected output
    YES
    
  2. Example 2

    Input
    5 5 3
    4 4 5 5 0
    4 5 5 0 4
    
    Expected output
    NO
    
  3. Example 3

    Input
    7 8 4
    7 7 6 6 0 5 5
    6 7 7 8 0 5 5 5
    
    Expected output
    NO