Language: C++

#include<bits/stdc++.h>
using namespace std;

const long long INF = 1e9;

vector<vector<int>> graph;
string val;

void dfs(int u, int &ans)
{
    int sum = 0;
    int ma = 0;
    vector<vector<int>> kek;
    for (int v: graph[u])
    {
        pair<int, int> tmp = _dfs(v, u);
        kek.push_back(tmp.first + tmp.second):
        sum += tmp.first + tmp.second;
        ma = max(ma, tmp.first + tmp.second);
    }
    
    if (sum & 1)
        return;
    else if (ma * 2 > sum)
        return;
    else
        ans = min(ans, sum / 2); 
}

pair<int, int> _dfs(int u, int p)
{
    pair<int, int> ret = {0, val[u] == '1'};
    
    for (int v: graph[u])
    {
        if (v == p)
            continue;
        
        pair<int, int> tmp = _dfs(v, u);
        ret.first += tmp.first + tmp.second;
        ret.second += tmp.second;
    }
    
    return ret;
}

int dp[10000000];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int n;
    cin >> n >> val;
    graph.resize(n);
    for (int i = 1; i != n; ++i)
    {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    
    int ans = INF;
    int u = -1;
    for (int i = 0; i != n; ++i)
        dfs(i, ans);
    
    if (ans == INF)
        cout << -1 << '\n';
    else
        cout << ans << '\n';
    
    return 0;
}

Compiler output:
a.cc: In function 'void dfs(int, int&)':
a.cc:16:30: error: '_dfs' was not declared in this scope; did you mean 'dfs'?
   16 |         pair<int, int> tmp = _dfs(v, u);
      |                              ^~~~
      |                              dfs
a.cc:17:22: error: no matching function for call to 'std::vector<std::vector<int> >::push_back(int)'
   17 |         kek.push_back(tmp.first + tmp.second):
      |         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; value_type = std::vector<int>]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'int' to 'const std::vector<std::vector<int> >::value_type&' {aka 'const std::vector<int>&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; value_type = std::vector<int>]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'int' to 'std::vector<std::vector<int> >::value_type&&' {aka 'std::vector<int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~