Language: C++

#include<cstdio>
#include<iostream>
#define RI register int
#define CI const int&
using namespace std;
const int N=2005,INF=1e9;
struct edge
{
	int to,nxt;
}e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
inline void addedge(CI x,CI y)
{
	e[++cnt]=(edge){y,head[x]}; head[x]=cnt;
	e[++cnt]=(edge){x,head[y]}; head[y]=cnt;
}
#define to e[i].to
inline void DFS(CI now,CI fa=0)
{
	RI i; f[now]=g[now]=0; size[now]=s[now]-'0'; 
	for (i=head[now];i;i=e[i].nxt) if (to!=fa)
	DFS(to,now),f[now]+=f[to]+size[to],g[now]+=g[to]+size[to],size[now]+=size[to];
	int x,y; for (i=head[now];i;i=e[i].nxt) if (to!=fa)
	if ((x=g[to]+size[to])>(y=f[now]-(f[to]+size[to]))) return (void)(g[now]=x-y); g[now]&=1;
}
#undef to
int main()
{
	RI i; for (scanf("%d%s",&n,s+1),i=1;i<n;++i)
	scanf("%d%d",&x,&y),addedge(x,y); for (i=1;i<=n;++i)
	if (DFS(i),!g[i]) ans=min(ans,f[i]>>1);
	if (ans==INF) puts("-1"); else printf("%d",ans); return 0;
}

Compiler output:
a.cc: In function 'void DFS(const int&, const int&)':
a.cc:19:12: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   19 |         RI i; f[now]=g[now]=0; size[now]=s[now]-'0';
      |            ^
a.cc:19:32: error: reference to 'size' is ambiguous
   19 |         RI i; f[now]=g[now]=0; size[now]=s[now]-'0';
      |                                ^~~~
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:2:
/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:38: note:                 'int size [2005]'
   10 | }e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
      |                                      ^~~~
a.cc:21:35: error: reference to 'size' is ambiguous
   21 |         DFS(to,now),f[now]+=f[to]+size[to],g[now]+=g[to]+size[to],size[now]+=size[to];
      |                                   ^~~~
/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:38: note:                 'int size [2005]'
   10 | }e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
      |                                      ^~~~
a.cc:21:58: error: reference to 'size' is ambiguous
   21 |         DFS(to,now),f[now]+=f[to]+size[to],g[now]+=g[to]+size[to],size[now]+=size[to];
      |                                                          ^~~~
/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:38: note:                 'int size [2005]'
   10 | }e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
      |                                      ^~~~
a.cc:21:67: error: reference to 'size' is ambiguous
   21 |         DFS(to,now),f[now]+=f[to]+size[to],g[now]+=g[to]+size[to],size[now]+=size[to];
      |                                                                   ^~~~
/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:38: note:                 'int size [2005]'
   10 | }e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
      |                                      ^~~~
a.cc:21:78: error: reference to 'size' is ambiguous
   21 |         DFS(to,now),f[now]+=f[to]+size[to],g[now]+=g[to]+size[to],size[now]+=size[to];
      |                                                                              ^~~~
/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:38: note:                 'int size [2005]'
   10 | }e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
      |                                      ^~~~
a.cc:23:22: error: reference to 'size' is ambiguous
   23 |         if ((x=g[to]+size[to])>(y=f[now]-(f[to]+size[to]))) return (void)(g[now]=x-y); g[now]&=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:38: note:                 'int size [2005]'
   10 | }e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
      |                                      ^~~~
a.cc:23:49: error: reference to 'size' is ambiguous
   23 |         if ((x=g[to]+size[to])>(y=f[now]-(f[to]+size[to]))) return (void)(g[now]=x-y); g[now]&=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:38: note:                 'int size [2005]'
   10 | }e[N<<1]; int n,head[N],cnt,x,y,f[N],size[N],g[N],ans=INF; char s[N];
      |                                      ^~~~
a.cc: In function 'int main()':
a.cc:28:12: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   28 |         RI i; for (scanf("%d%s",&n,s+1),i=1;i<n;++i)
      |            ^