Language: C++

#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<set>
using namespace std;
int n,prt[300005],w[300005],fa[300005],size[300005],sums[300005];
long long ans=0;
int read()
{
	char c=getchar();
	int res=0;
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9') res=(res<<1)+(res<<3)+(c^48),c=getchar();
	return res;
}
int find(int x)
{
	return (x==fa[x])?x:fa[x]=find(fa[x]);
}
struct que
{
	int id;
	long long v,num;
	friend bool operator <(const que &a,const que &b)
	{
		if(a.v*b.num==b.v*a.num) return a.id<b.id;
		return a.v*b.num>b.v*a.num;
	}
};
set<que> s;
set<que>::iterator it;
int main()
{
	n=read();
	for(int i=2;i<=n;i++) prt[i]=read();
	for(int i=1;i<=n;i++) w[i]=read();
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=2;i<=n;i++)
	{
		if(!w[i])
		{
			fa[i]=find(prt[i]),size[fa[i]]++;
			if(fa[i]==1&&!w[1]) continue;
			ans++;
		}
	}
	for(int i=1;i<=n;i++) if(w[i]) sums[i]=1;
	for(int i=2;i<=n;i++) if(w[i]) s.insert((que){i,size[i],sums[i]});
	int sz=s.size();
	while(sz)
	{
		it=s.begin();s.erase(it);
		int u=(*it).id;
		int fas=find(prt[u]);
		ans+=1ll*sums[fas]*size[u];
		if(fas!=1)
		{
			que y=(que){fas,size[fas],sums[fas]};s.erase(y);
			sums[fas]+=sums[u];size[fas]+=size[u];fa[u]=fas;
			y=(que){fas,size[fas],sums[fas]};s.insert(y);
		}
		else sums[fas]+=sums[u],size[fas]+=size[u],fa[u]=fas;
		sz--;
	}
	printf("%lld\n",ans);
}

Compiler output:
a.cc: In function 'int main()':
a.cc:46:44: error: reference to 'size' is ambiguous
   46 |                         fa[i]=find(prt[i]),size[fa[i]]++;
      |                                            ^~~~
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:10:40: note:                 'int size [300005]'
   10 | int n,prt[300005],w[300005],fa[300005],size[300005],sums[300005];
      |                                        ^~~~
a.cc:52:53: error: expected primary-expression before ')' token
   52 |         for(int i=2;i<=n;i++) if(w[i]) s.insert((que){i,size[i],sums[i]});
      |                                                     ^
a.cc:59:36: error: reference to 'size' is ambiguous
   59 |                 ans+=1ll*sums[fas]*size[u];
      |                                    ^~~~
/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:10:40: note:                 'int size [300005]'
   10 | int n,prt[300005],w[300005],fa[300005],size[300005],sums[300005];
      |                                        ^~~~
a.cc:62:35: error: expected primary-expression before ')' token
   62 |                         que y=(que){fas,size[fas],sums[fas]};s.erase(y);
      |                                   ^
a.cc:63:44: error: reference to 'size' is ambiguous
   63 |                         sums[fas]+=sums[u];size[fas]+=size[u];fa[u]=fas;
      |                                            ^~~~
/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:10:40: note:                 'int size [300005]'
   10 | int n,prt[300005],w[300005],fa[300005],size[300005],sums[300005];
      |                                        ^~~~
a.cc:63:55: error: reference to 'size' is ambiguous
   63 |                         sums[fas]+=sums[u];size[fas]+=size[u];fa[u]=fas;
      |                                                       ^~~~
/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:10:40: note:                 'int size [300005]'
   10 | int n,prt[300005],w[300005],fa[300005],size[300005],sums[300005];
      |                                        ^~~~
a.cc:64:31: error: expected primary-expression before ')' token
   64 |                         y=(que){fas,size[fas],sums[fas]};s.insert(y);
      |                               ^
a.cc:66:41: error: reference to 'size' is ambiguous
   66 |                 else sums[fas]+=sums[u],size[fas]+=size[u],fa[u]=fas;
      |                                         ^~~~
/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:10:40: note:                 'int size [300005]'
   10 | int n,prt[300005],w[300005],fa[300005],size[300005],sums[300005];
      |                                        ^~~~
a.cc:66:52: error: reference to 'size' is ambiguous
   66 |                 else sums[fas]+=sums[u],size[fas]+=size[u],fa[u]=fas;
      |                                                    ^~~~
/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:10:40: note:                 'int size [300005]'
   10 | int n,prt[300005],w[300005],fa[300005],size[300005],sums[300005];
      |                                        ^~~~