Problem:
Score : 2000 points

Problem Statement

You are given a rooted tree with N vertices.
The vertices are numbered 0, 1, ..., N-1.
The root is Vertex 0, and the parent of Vertex i (i = 1, 2, ..., N-1) is Vertex p_i.

Initially, an integer a_i is written in Vertex i.
Here, (a_0, a_1, ..., a_{N-1}) is a permutation of (0, 1, ..., N-1).

You can execute the following operation at most 25 000 times. Do it so that the value written in Vertex i becomes i.

Choose a vertex and call it v. Consider the path connecting Vertex 0 and v.

Rotate the values written on the path. That is, For each edge (i, p_i) along the path, replace the value written in Vertex p_i with the value written in Vertex i (just before this operation), and replace the value of v with the value written in Vertex 0 (just before this operation).

You may choose Vertex 0, in which case the operation does nothing.

Constraints

2 \leq N \leq 2000

0 \leq p_i \leq i-1

(a_0, a_1, ..., a_{N-1}) is a permutation of (0, 1, ..., N-1).

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_{N-1}
a_0 a_1 ... a_{N-1}

Output

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

Sample Input 1

5
0 1 2 3
2 4 0 1 3

Sample Output 1

2
3
4

After the first operation, the values written in Vertex 0, 1, .., 4 are 4, 0, 1, 2, 3.

Sample Input 2

5
0 1 2 2
4 3 1 2 0

Sample Output 2

3
4
3
1

After the first operation, the values written in Vertex 0, 1, .., 4 are 3, 1, 0, 2, 4.

After the second operation, the values written in Vertex 0, 1, .., 4 are 1, 0, 2, 3, 4.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>

#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-funroll-loops")
#pragma GCC target("avx,sse4")

#define mms(a,n) memset(a,0,sizeof((a)[0])*(n))
#define mmp(a,b,n) memcpy(a,b,sizeof((b)[0])*(n))
#define lowbit(x) ((x)&-(x))
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define fo(i,l,r) for(register int i=l,_lim_=r;i<=_lim_;i++)
#define fd(i,r,l) for(register int i=r,_lim_=l;i>=_lim_;i--)
#define fos(i,l,r,d) for(register int i=l,_lim_=r;i<=r;i+=d)
#define fol(i,l,r) for(register ll i=l,_lim_=r;i<=_lim_;i++)
#define fdl(i,r,l) for(register ll i=r,_lim_=l;i>=_lim_;i--)
#define fosl(i,l,r,d) for(register ll i=l,_lim_=r;i<=r;i+=d)
#define Clear(a) memset(a,0,sizeof(a))
#define Copy(a,b) memcpy(a,b,sizeof(b))
#define ALL(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ldb;
typedef double db;
typedef pair<int,ll> pi;

namespace io{
	const int L=(1<<21)+1;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,st[55];int f,tp;
	#ifdef whzzt
		#define gc() getchar()
	#else
		#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	#endif
	inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();}
	template<class I>
	inline void gi(I&x){
		for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
	template<class I>
	inline void print(I x){
		if(!x)putc('0');if(x<0)putc('-'),x=-x;
		while(x)st[++tp]=x%10+'0',x/=10;
		while(tp)putc(st[tp--]);
	}
	inline void gs(char*s,int&l){
		for(c=gc();c<'a'||c>'z';c=gc());
		for(l=0;c<='z'&&c>='a';c=gc())s[l++]=c;
	}
	inline char O(){
		for(c=gc();c!='C'&&c!='Q'&&c!='R';c=gc());
		return c;
	}
	inline void ps(const char*s){
		fo(i,0,strlen(s)-1)putc(s[i]);
	}
};
using io::putc;
using io::gi;
using io::gs;
using io::print;

const int N=2005;
int n,p[N],w[N],sz[N],son[N],bel[N],bot[N],m,pos[N];
bool del[N],red[N],vi[N];
vector<int>e[N],O,li[N];

void dfs(int u){
	sz[u]=1;
	for(int v:e[u]){
		dfs(v),sz[u]+=sz[v];
		if(sz[v]>sz[son[u]])son[u]=v;
	}
}
void dfs(int u,int b){
	bel[u]=b;li[b].pb(u);pos[u]=SZ(li[b]);
	if(u==b)red[u]=true;if(e[u].size()>1)red[b]=false;
	if(son[u])dfs(son[u],b);else bot[b]=u;
	for(int v:e[u])if(v!=son[u])dfs(v,v);
	if(red[u])for(int v:li[u])red[v]=true,m++;
}
void flip(int u){
	O.pb(u);for(int v=p[u];v;v=p[v])swap(w[v],w[u]);
}
int find(int u){
	bool o=true;
	for(int v:e[u]){
		int p=find(v);
		if(p>0)return p;
		o&=p==-1; 
	}
	if(o)return vi[w[u]]?-1:u;
	return 0;
}
void move(){
	int u=w[1];if(!vi[u])vi[u]=true;m-=red[u];
	if(red[u])for(int v=bot[bel[u]];v;v=p[v])if(!red[w[v]]||!vi[w[v]]||pos[w[v]]<=pos[u])return flip(v);
	flip(find(1));
}
bool fix(){
	fo(i,2,n)if(!del[i])e[p[i]].pb(i);
	if(!e[1].size())return false;
	dfs(1);dfs(1,1);
	while(m)move();
	fo(i,1,n)e[i].clear(),li[i].clear(),del[i]|=red[i],red[i]=vi[i]=bot[i]=pos[i]=bel[i]=son[i]=sz[i]=0;m=0;
	return true;
}

int main(){
	gi(n);
	fo(i,2,n)gi(p[i]),p[i]++;
	fo(i,1,n)gi(w[i]),w[i]++;
	while(fix());
	print(O.size());putc('\n');
	for(int v:O)print(v-1),putc('\n');
	io::flush();
	return 0;
}