Language: C++

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
//#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned int ui;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
    x=0; char c=gc(); int flag=0;
    while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
    while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
    if (flag) x=-x;
}
template <class T> T _max(T a,T b){return a>b ? a : b;}
template <class T> T _min(T a,T b){return a<b ? a : b;}
template <class T> bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
const int N=100005;
int n,par[N],col[N],cnt[2][N];
namespace dsu
{
	int father[N];
	void init()
	{
		for (int i=1;i<=n;++i)
		{
			father[i]=i;
		}
	}
	
	int getfather(int x)
	{
		return father[x]==x ? x : father[x]=getfather(father[x]);
	}
}

struct cmp
{
	bool operator () (const int &x,const int &y)
	{
		ll tmp1=(ll)cnt[0][x]*cnt[1][y];
		ll tmp2=(ll)cnt[1][x]*cnt[0][y];
		if (tmp1==tmp2) return x<y;
		else return tmp1>tmp2;
	}
};

void init()
{
	read(n);
	for (int i=2;i<=n;++i)
	{
		read(par[i]);
	}
	for (int i=1;i<=n;++i)
{
		read(col[i]);
	}
	dsu::init();
}

void solve()
{
	multiset <int,cmp> S;
	multiset <int,cmp>::iterator it;
	for (int i=1;i<=n;++i)
	{
		if (col[i]) cnt[1][i]=1,cnt[0][i]=0;
		else cnt[0][i]=1,cnt[1][i]=0;
		if (i>1) S.insert(i);
	}
	ll ans=0;
	for (int rnd=1;rnd<n;++rnd)
	{
		int x=*S.begin(); 
		S.erase(x);
		int y=dsu::getfather(par[x]);
		S.erase(y);
		//out(x); outln(y);
		ans+=(ll)cnt[1][y]*cnt[0][x];
		cnt[0][y]+=cnt[0][x]; cnt[1][y]+=cnt[1][x];
		dsu::father[x]=y;
		if (y>1) S.insert(y);
	}
	printf("%lld\n",ans);
}

int main()
{
	init();
	solve();
    return 0;
}


Compiler output:
In file included from /usr/include/c++/14/set:62,
                 from a.cc:11:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>; _Const_Link_type = const std::_Rb_tree_node<int>*]':
/usr/include/c++/14/bits/stl_tree.h:2147:44:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_equal_pos(const key_type&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>; key_type = int]'
 2147 |           __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
      |                                             ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2196:4:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_Arg&&) [with _Arg = const int&; _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = cmp; _Alloc = std::allocator<int>; iterator = std::_Rb_tree<int, int, std::_Identity<int>, cmp, std::allocator<int> >::iterator]'
 2196 |         = _M_get_insert_equal_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:505:36:   required from 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Compare = cmp; _Alloc = std::allocator<int>; iterator = std::_Rb_tree<int, int, std::_Identity<int>, cmp, std::allocator<int> >::const_iterator; value_type = int]'
  505 |       { return _M_t._M_insert_equal(__x); }
      |                ~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:88:20:   required from here
   88 |                 if (i>1) S.insert(i);
      |                          ~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const cmp&, const int&, const int&>' evaluates to false