Under construction

Still being built. Things may change

Citadel Hierarchy

Interview

Time limit1sMemory limit1024 MB

Summary
Given a rooted tree of Ricks with Mortys as leaves, decide whether each Rick can reorder its direct subordinates so the bottom-level Mortys end up sorted.
Level

Medium5 of 10

Topics
Tree, DFS, Implementation
Solved
No attempts yet

Problem

As is well known, the Citadel of Ricks is home to countless Ricks and Mortys: exactly nn Ricks and mm Mortys. To keep the new Citadel from descending into total chaos, it was decided to build a clear hierarchy that makes it possible to quickly determine who is to blame for any incident.

First, the Ricks are numbered from 1 to nn in decreasing order of importance. The Mortys are numbered from 1 to mm in increasing order of unimportance. The hierarchy is drawn as a rooted tree with these properties:

  • The tree has exactly nn internal vertices and mm leaves, and all leaves are at the same depth.
  • All internal vertices are occupied by Ricks, and all leaves are occupied by Mortys. All Mortys must be at the bottom of the hierarchy.
  • Ricks on any level have smaller numbers than Ricks on the levels below. In particular, the root holds the Supreme Rick, number 1.
  • On each level, the Ricks are numbered in increasing order from left to right.
  • Every Rick on the levels up to the second-to-last level has at least two direct subordinates.

The Ricks built this hierarchy quickly. The Mortys, in a panic, lined up on the bottom level in a random order. Seeing this chaos, the Ricks decided that since they do not like rules much anyway, the rule that Ricks on each level must be ordered can be dropped. The Mortys on the bottom level, however, must be sorted in increasing order from left to right.

Each Rick may rearrange its direct subordinates in any way. A Rick may not change its set of subordinates (taking new ones or handing old ones to another Rick), and may not move to another level. Can the Ricks sort all the Mortys in increasing order this way? An example of a possible solution is shown below:

The initial hierarchy and the moves needed to sort the Mortys.

Input

The first line contains two integers nn and mm separated by a space (2⩽n,m⩽1052 \leqslant n, m \leqslant 10^5).

The second line contains mm distinct integers aia_i separated by spaces: the numbers of the Mortys in the order they stand in the initial hierarchy, from left to right (1⩽ai⩽m1 \leqslant a_i \leqslant m).

The third line contains p2,…,pnp_2, \ldots, p_n, the numbers of the immediate superiors of Ricks numbered 2 through nn (1⩽pi<i1 \leqslant p_i < i).

The fourth line contains, in the same format, q1,…,qmq_1, \ldots, q_m, the numbers of the immediate superiors of the Mortys in the order they stand in the initial hierarchy (1⩽qi⩽n1 \leqslant q_i \leqslant n). Note that q1q_1 is the superior of the Morty with number a1a_1, not the Morty with number 1.

It is guaranteed that the hierarchy structure satisfies the constraints above.

Output

Print YES if the Ricks can sort all the Mortys in increasing order by swapping their direct subordinates, and NO otherwise.

Examples2

  1. Example 1

    Input
    4 4
    1 3 2 4
    1 1 1
    2 2 3 4
    
    Expected output
    NO
    
  2. Example 2

    Input
    8 10
    10 9 8 3 4 5 7 6 1 2
    1 1 2 2 3 3 3
    4 5 5 6 6 7 7 7 8 8
    
    Expected output
    YES