Language: C++

#include<set>
#include<cstdio>
#include<cstring>
#define N 200010
#define ll long long
using namespace std;
void read(int&x)
{
	x = 0;char s = getchar();
	while(s<'0'||s>'9')s = getchar();
	while(s>='0'&&s<='9')x = (x<<3)+(x<<1)+s-48,s = getchar();
}
int n,p[N],f[N],w[N],n0[N],n1[N];
ll ans;
int fd(int x){return x==f[x]?x:f[x] = fd(f[x]);}
struct data
{
	int id,n0,n1;
	friend bool operator<(data a,data b)
	{
		if(1ll*a.n0*b.n1==1ll*a.n1*b.n0)
		{
			if(a.n0==b.n0&&a.n1==b.n1)return a.id<b.id;
			if(a.n0==b.n0)return a.n1<b.n1;
			return a.n0>b.n0;
		}return 1ll*a.n0*b.n1>1ll*a.n1*b.n0;
	}
};
set<data>s;
set<data>::iterator it;
int main()
{
	read(n);
	for(int i = 2;i<=n;i++)read(p[i]);
	for(int i = 1;i<=n;i++)read(w[i]),f[i] = i,w[i]?n1[i] = 1:n0[i] = 1;
	for(int i = 2;i<=n;i++)s.insert((data){i,n0[i],n1[i]});
	for(int i = 1;i<n;i++)
	{
		it = s.begin();
		int u = (*it).id,v = fd(p[u]);
		s.erase(*it);
		if(v^1)s.erase((data){v,n0[v],n1[v]});
		ans+=1ll*n1[v]*n0[u];
		f[u] = v,n0[v]+=n0[u],n1[v]+=n1[u];
		if(v^1)s.insert((data){v,n0[v],n1[v]});
	}printf("%lld\n",ans);
	return 0;
}

Compiler output:
a.cc:29:9: error: template argument 1 is invalid
   29 | set<data>s;
      |         ^
a.cc:29:9: error: template argument 2 is invalid
a.cc:29:9: error: template argument 3 is invalid
a.cc:30:9: error: template argument 1 is invalid
   30 | set<data>::iterator it;
      |         ^
a.cc:30:9: error: template argument 2 is invalid
a.cc:30:9: error: template argument 3 is invalid
a.cc:30:21: error: expected initializer before 'it'
   30 | set<data>::iterator it;
      |                     ^~
a.cc: In function 'int main()':
a.cc:36:34: error: request for member 'insert' in 's', which is of non-class type 'int'
   36 |         for(int i = 2;i<=n;i++)s.insert((data){i,n0[i],n1[i]});
      |                                  ^~~~~~
a.cc:36:42: error: reference to 'data' is ambiguous
   36 |         for(int i = 2;i<=n;i++)s.insert((data){i,n0[i],n1[i]});
      |                                          ^~~~
In file included from /usr/include/c++/14/set:65,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:8: note:                 'struct data'
   16 | struct data
      |        ^~~~
a.cc:39:17: error: 'it' was not declared in this scope; did you mean 'i'?
   39 |                 it = s.begin();
      |                 ^~
      |                 i
a.cc:39:24: error: request for member 'begin' in 's', which is of non-class type 'int'
   39 |                 it = s.begin();
      |                        ^~~~~
a.cc:41:19: error: request for member 'erase' in 's', which is of non-class type 'int'
   41 |                 s.erase(*it);
      |                   ^~~~~
a.cc:42:20: error: 'v' was not declared in this scope
   42 |                 if(v^1)s.erase((data){v,n0[v],n1[v]});
      |                    ^
a.cc:42:26: error: request for member 'erase' in 's', which is of non-class type 'int'
   42 |                 if(v^1)s.erase((data){v,n0[v],n1[v]});
      |                          ^~~~~
a.cc:42:33: error: reference to 'data' is ambiguous
   42 |                 if(v^1)s.erase((data){v,n0[v],n1[v]});
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:8: note:                 'struct data'
   16 | struct data
      |        ^~~~
a.cc:43:29: error: 'v' was not declared in this scope
   43 |                 ans+=1ll*n1[v]*n0[u];
      |                             ^
a.cc:45:26: error: request for member 'insert' in 's', which is of non-class type 'int'
   45 |                 if(v^1)s.insert((data){v,n0[v],n1[v]});
      |                          ^~~~~~
a.cc:45:34: error: reference to 'data' is ambiguous
   45 |                 if(v^1)s.insert((data){v,n0[v],n1[v]});
      |                                  ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:16:8: note:                 'struct data'
   16 | struct data
      |        ^~~~