Language: C++

#include<bits/stdc++.h>
#define deb(x) cerr << #x" = "<<x <<endl;
using namespace std;
const int maxn=2020;

int n;
int siz[maxn],son[maxn];
int fa[maxn],top[maxn];
int a[maxn],dep[maxn];
vector<int>G[maxn];
vector<int>member[maxn];
int topdeg[maxn];
void dfs(int u){
	siz[u]=1;
	for(auto v:G[u]){
		dep[v]=dep[u]+1;
		dfs(v);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
			son[u]=v;
	}
}
void build(int u,int tp){
	top[u]=tp;
	member[top[u]].push_back(u);
	if(son[u])build(son[u],tp);
	for(auto v:G[u])if(v!=son[u]){
		build(v,v);
		//topdeg[son[u]]++;
		topdeg[top[u]]++;
	}
}



vector<int>Oper;
void rot(int v){
	vector<int>path;
	int u=v;
	while(u){
		path.push_back(u);
		u=fa[u];
	}
	static int b[maxn];
	for(int i=0;i<path.size();i++)
		b[i]=a[path[i]];
	for(int i=0;i<path.size();i++)
		a[path[i]]=b[(i+path.size()-1)%path.size()];
	Oper.push_back(v);
}

int hole[maxn],done[maxn],ind[maxn];
vector<int>cur[maxn];
set<int,cmp>bc;
void insert(int x){
	int tp=top[x];
	int t=cur[tp].size();
	for(int i=0;i<cur[tp].size();i++){
		if(ind[cur[tp][i]]>ind[x]){
			t=i;
			break;
		}
	}
	int pos=(int)member[tp].size()-1-cur[tp].size()+t;
	rot(member[tp][pos]);
	bc.erase(member[tp][pos]);
	cur[tp].insert(cur[tp].begin()+t,x);
}

bool bydep(int x,int y){
	return dep[x] < dep[y];
}

struct cmp{
	bool operator()(int x,int y){return bydep(x,y);}	
};

void work(){
	int t=n;

	memset(hole,0,sizeof hole);
	bc.clear();

	for(int i=1;i<=n;i++)if(top[i]==i&&!done[i]){
		if(topdeg[i]==0){
			topdeg[top[fa[i]]]--;
			for(auto v:member[top[i]]){
				hole[v]=1;
				done[v]=1;
				//cerr<<"hole "<<v<<endl;
			}
		}
	}
	
	for(int i=1;i<=n;i++)if(!done[i])
		bc.insert(i);


	while(t--){
		//cerr<<"a[1] = "<<a[1]<<endl;

		if(hole[a[1]]){
			insert(a[1]);
		}else{
			rot(*bc.rbegin());	
			bc.erase(bc.rbegin());
		}
	}
}

int main(){
	scanf("%d",&n);
	for(int i=2;i<=n;i++){
		scanf("%d",&fa[i]);
		fa[i]++;
		G[fa[i]].push_back(i);
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[i]++;
	}
	dfs(1);
	build(1,1);
	for(int i=1;i<=n;i++){
		for(int j=0;j<member[i].size();j++)
			ind[member[i][j]]=j;
	}


	while(1){
		int ok=1;
		for(int i=1;i<=n;i++)if(a[i]!=i){
			ok=0;
			break;
		}
		if(ok)break;

		//cerr<<"new round"<<endl;
		work();
	}

	cout<<Oper.size()<<endl;
	for(auto x:Oper)
		printf("%d\n",x-1);

	return 0;
}

Compiler output:
a.cc:54:9: error: 'cmp' was not declared in this scope; did you mean 'bcmp'?
   54 | set<int,cmp>bc;
      |         ^~~
      |         bcmp
a.cc:54:12: error: template argument 2 is invalid
   54 | set<int,cmp>bc;
      |            ^
a.cc: In function 'void insert(int)':
a.cc:66:12: error: request for member 'erase' in 'bc', which is of non-class type 'int'
   66 |         bc.erase(member[tp][pos]);
      |            ^~~~~
a.cc: In function 'void work()':
a.cc:82:12: error: request for member 'clear' in 'bc', which is of non-class type 'int'
   82 |         bc.clear();
      |            ^~~~~
a.cc:96:20: error: request for member 'insert' in 'bc', which is of non-class type 'int'
   96 |                 bc.insert(i);
      |                    ^~~~~~
a.cc:105:33: error: request for member 'rbegin' in 'bc', which is of non-class type 'int'
  105 |                         rot(*bc.rbegin());
      |                                 ^~~~~~
a.cc:106:28: error: request for member 'erase' in 'bc', which is of non-class type 'int'
  106 |                         bc.erase(bc.rbegin());
      |                            ^~~~~
a.cc:106:37: error: request for member 'rbegin' in 'bc', which is of non-class type 'int'
  106 |                         bc.erase(bc.rbegin());
      |                                     ^~~~~~