Language: C++

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define maxn 2005
#define Rint register int
#define INF 0x3f3f3f3f
using namespace std;
typedef long long lxl;

template <typename T>
inline void read(T &x)
{
	x=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	x*=f;
}

struct edge
{
	int u,v,next;
}e[maxn<<1];

int head[maxn],k;

inline void add(int u,int v)
{
	e[k]=(edge){u,v,head[u]};
	head[u]=k++;
}

int n,a[maxn];
int size[maxn],dis[maxn],f[maxn];

inline void dfs(int u,int fa)
{
	size[u]=a[u];
	dis[u]=0;
	int son=-1;
	for(int i=head[u];~i;i=e[i].next)
	{
		int v=e[i].v;
		if(v==fa) continue;
		dfs(v,u);
		size[u]+=size[v];
		dis[u]+=(dis[v]+=size[v]);
		if(!~son||dis[son]<dis[v]) son=v;
	}
	if(!~son) return void(f[u]=0);
	if(dis[u]-dis[son]>=dis[son]) f[u]=dis[u]/2;
	else f[u]=dis[u]-dis[son]+min(f[son]*2,2*dis[son]-dis[u])/2;
}

char s[maxn];

int main()
{
	read(n);
	scanf(" %s",s+1);
	for(int i=1;i<=n;++i) a[i]=s[i]-'0';
	memset(head,-1,sizeof(head));
	for(int i=1,u,v;i<n;++i)
	{
		read(u),read(v);
		add(u,v);add(v,u);
	}
	int ans=-1;
	for(int i=1;i<=n;++i)
	{
		dfs(i,0);
		if(f[i]*2==dis[i]) ans=(!~ans||ans>f[i])?f[i]:ans;
	}
	printf("%d\n",ans);
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:39:9: error: reference to 'size' is ambiguous
   39 |         size[u]=a[u];
      |         ^~~~
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:35:5: note:                 'int size [2005]'
   35 | int size[maxn],dis[maxn],f[maxn];
      |     ^~~~
a.cc:47:17: error: reference to 'size' is ambiguous
   47 |                 size[u]+=size[v];
      |                 ^~~~
/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:35:5: note:                 'int size [2005]'
   35 | int size[maxn],dis[maxn],f[maxn];
      |     ^~~~
a.cc:47:26: error: reference to 'size' is ambiguous
   47 |                 size[u]+=size[v];
      |                          ^~~~
/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:35:5: note:                 'int size [2005]'
   35 | int size[maxn],dis[maxn],f[maxn];
      |     ^~~~
a.cc:48:34: error: reference to 'size' is ambiguous
   48 |                 dis[u]+=(dis[v]+=size[v]);
      |                                  ^~~~
/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:35:5: note:                 'int size [2005]'
   35 | int size[maxn],dis[maxn],f[maxn];
      |     ^~~~