Language: C++

#include<bits/stdc++.h>
#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:27:9: error: template argument 1 is invalid
   27 | set<data>s;
      |         ^
a.cc:27:9: error: template argument 2 is invalid
a.cc:27:9: error: template argument 3 is invalid
a.cc:28:9: error: template argument 1 is invalid
   28 | set<data>::iterator it;
      |         ^
a.cc:28:9: error: template argument 2 is invalid
a.cc:28:9: error: template argument 3 is invalid
a.cc:28:21: error: expected initializer before 'it'
   28 | set<data>::iterator it;
      |                     ^~
a.cc: In function 'int main()':
a.cc:34:34: error: request for member 'insert' in 's', which is of non-class type 'int'
   34 |         for(int i = 2;i<=n;i++)s.insert((data){i,n0[i],n1[i]});
      |                                  ^~~~~~
a.cc:34:42: error: reference to 'data' is ambiguous
   34 |         for(int i = 2;i<=n;i++)s.insert((data){i,n0[i],n1[i]});
      |                                          ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 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:14:8: note:                 'struct data'
   14 | struct data
      |        ^~~~
a.cc:37:17: error: 'it' was not declared in this scope; did you mean 'i'?
   37 |                 it = s.begin();
      |                 ^~
      |                 i
a.cc:37:24: error: request for member 'begin' in 's', which is of non-class type 'int'
   37 |                 it = s.begin();
      |                        ^~~~~
a.cc:39:19: error: request for member 'erase' in 's', which is of non-class type 'int'
   39 |                 s.erase(*it);
      |                   ^~~~~
a.cc:40:20: error: 'v' was not declared in this scope
   40 |                 if(v^1)s.erase((data){v,n0[v],n1[v]});
      |                    ^
a.cc:40:26: error: request for member 'erase' in 's', which is of non-class type 'int'
   40 |                 if(v^1)s.erase((data){v,n0[v],n1[v]});
      |                          ^~~~~
a.cc:40:33: error: reference to 'data' is ambiguous
   40 |                 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:14:8: note:                 'struct data'
   14 | struct data
      |        ^~~~
a.cc:41:29: error: 'v' was not declared in this scope
   41 |                 ans+=1ll*n1[v]*n0[u];
      |                             ^
a.cc:43:26: error: request for member 'insert' in 's', which is of non-class type 'int'
   43 |                 if(v^1)s.insert((data){v,n0[v],n1[v]});
      |                          ^~~~~~
a.cc:43:34: error: reference to 'data' is ambiguous
   43 |                 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:14:8: note:                 'struct data'
   14 | struct data
      |        ^~~~