Language: C++

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

vector<int> edge[108000];
int size[108000];
vector<int> edges[108000];
vector<int> nodes[108000];
int before[108000];
int next[108000];
bool done[108000];
int pp, n, gr;
int p[108000], a[108000];
int dep[108000];
int ord[108000];
int group[108000];
int pos[108000];
vector<int> ans;
void dfs(int x){
	size[x] = 1;
	next[x] = -1;
	nodes[dep[x]].push_back(x);
	for(int i = 0;i < edge[x].size();i++){
		int to = edge[x][i];
		dep[to] = dep[x] + 1;
		dfs(to);
		size[x] += size[to];
	}
	for(int i = 0;i < edge[x].size();i++){
		int to = edge[x][i];										 
		if(size[to] * 2 >= size[x]){
			next[x] = to;
			before[to] = x;
		}
		else{
			before[to] = -1;
		}
	}
}

void go(int x){
	ans.push_back(x);
	int tmp = a[x];
	a[x] = a[0];
	while(x){
		swap(tmp, a[p[x]]);
		x = p[x];
	}
}

int good_place(int x = 0){
	for(int i = 0;i < edge[x].size();i++){
		int to = edge[x][i];
		if(done[a[to]])continue;
		return good_place(to);
	}
	return x;
}

void solve(int d){
	int rest = 0;
	for(int i = 0;i < n;i++){
		done[i] = false;
		if(ord[group[i]] <= d)rest++;
		else done[i] = true;
	}
	for(int i = 0;i < rest;i++){
		int now = a[0];
		if(ord[group[now]] != d){
			int nxt = good_place();
			done[now] = true;
			go(nxt);
		}
		else{
			gr = group[now];
			done[now] =true;
			for(int i = edges[gr].size() - 1;i >= 0;i--){
				int tmpp = edges[gr][i];
				int tmp = a[tmpp];
				if(group[tmp] != gr || !done[tmp]){
					go(tmpp);
					break;
				}
				if(pos[tmp] < pos[now]){
					go(tmpp);
					break;
				}
			}
		}
	}
}

int main(){
	cin >> n;
	fill(before, before + n, -1);
	for(int i = 1;i < n;i++){
		cin >> p[i];
		edge[p[i]].push_back(i);
	}
	for(int i = 0;i < n;i++)cin >> a[i];
	dfs(0);


	for(int d = 0;d < n;d++){
		for(int i = 0;i < nodes[d].size();i++){
			int n = nodes[d][i];
			if(before[n] == -1){
				group[n] = pp++;
				if(d != 0){
					ord[group[n]] = ord[group[p[n]]] + 1;
				}
			}
			else group[n] = group[before[n]];
			pos[n] = edges[group[n]].size();
			edges[group[n]].push_back(n);
		}
	}

	for(int i = 11;i >= 0;i--){
		
		solve(i);
	}
	cout << ans.size() << endl;
	for(int i = 0;i < ans.size();i++){
		cout << ans[i] << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(int)':
a.cc:21:9: error: reference to 'size' is ambiguous
   21 |         size[x] = 1;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [108000]'
    7 | int size[108000];
      |     ^~~~
a.cc:22:9: error: reference to 'next' is ambiguous
   22 |         next[x] = -1;
      |         ^~~~
In file included from /usr/include/c++/14/string:47:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [108000]'
   11 | int next[108000];
      |     ^~~~
a.cc:28:17: error: reference to 'size' is ambiguous
   28 |                 size[x] += size[to];
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [108000]'
    7 | int size[108000];
      |     ^~~~
a.cc:28:28: error: reference to 'size' is ambiguous
   28 |                 size[x] += size[to];
      |                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [108000]'
    7 | int size[108000];
      |     ^~~~
a.cc:32:20: error: reference to 'size' is ambiguous
   32 |                 if(size[to] * 2 >= size[x]){
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [108000]'
    7 | int size[108000];
      |     ^~~~
a.cc:32:36: error: reference to 'size' is ambiguous
   32 |                 if(size[to] * 2 >= size[x]){
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:7:5: note:                 'int size [108000]'
    7 | int size[108000];
      |     ^~~~
a.cc:33:25: error: reference to 'next' is ambiguous
   33 |                         next[x] = to;
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:5: note:                 'int next [108000]'
   11 | int next[108000];
      |     ^~~~