Generalized Insertion Sort

아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

You are given a rooted tree with NN vertices. The vertices are numbered 0,1,,N10, 1, \ldots, N-1. The root is vertex 00, and the parent of vertex ii (i=1,2,,N1)(i = 1, 2, \ldots, N-1) is Vertex p_ip\_i.

Initially, an integer a_ia\_i is written in vertex ii. Here, (a_0,a_1,,a_N1)(a\_0, a\_1, \ldots, a\_{N-1}) is a permutation of (0,1,,N1)(0, 1, \ldots, N-1).

You can execute the following operation at most 25,00025\\,000 times. The goal is to make the value written in vertex ii equal to ii.

  • Choose a vertex and call it vv. Consider the path connecting vertex 00 and vv.
  • Rotate the values written on the path. That is, For each edge (i,p_i)(i, p\_i) along the path, replace the value written in vertex p_ip\_i with the value written in vertex ii (just before this operation), and replace the value of vv with the value written in vertex 00 (just before this operation).
  • You may choose vertex 00, in which case the operation does nothing.

입력

Input is given in the following format:

NN

p_1p\_1 p_2p\_2 ... p_N1p\_{N-1}

a_0a\_0 a_1a\_1 ... a_N1a\_{N-1}

출력

In the first line, print the number of operations, QQ. In the second through (Q+1)(Q+1)-th lines, print the chosen vertices in order.

제한

2N20002 \leq N \leq 2000, 0p_ii10 \leq p\_i \leq i-1, (a_0,a_1,,a_N1)(a\_0, a\_1, \ldots, a\_{N-1}) is a permutation of (0,1,,N1)(0, 1, \ldots, N-1).

힌트

In Sample 1, after the first operation, the values written in vertex 0,1,,40, 1, \ldots, 4 are 4,0,1,2,34, 0, 1, 2, 3.

In Sample 2, after the first operation, the values written in vertex 0,1,,40, 1, \ldots, 4 are 3,1,0,2,43, 1, 0, 2, 4. After the second operation, the values written in vertex 0,1,,40, 1, \ldots, 4 are 1,0,2,3,41, 0, 2, 3, 4.