Language: C++

#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <functional>
using namespace std;
using T=tuple<int,int,int>;

int main(){
    int N;
    string S;
    cin>>N;
    cin>>S;
    vector<vector<int>> G(N);
    for(int i=0;i<N-1;i++){
        int a,b;
        cin>>a>>b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    int ans=1000000000;
    function<T(int,int)> dfs = [&](int v,int prev){
        int children=0,dist=0;
        if(S[v]=='1') children++;
        for(auto to:G[v]){
            if(to==prev) continue;
            int c,d;
            tie(c,d)=dfs(to, v);
            children+=c;
            dist+=c+d;
        }
        return P(children,dist);
    };
    for(int root=0;root<N;root++){
        vector<int> V;
        int sum=0;
        for(auto to:G[root]){
            int c,d;
            tie(c,d)=dfs(to,root);
            V.push_back(c);
            sum+=c+d;
        }
        if(sum%2) continue;
        ans=min(ans,sum/2);
    }
    if(ans==1000000000)ans=-1;
    cout<<ans<<endl;
    return 0;
}

Compiler output:
a.cc: In lambda function:
a.cc:29:31: error: no match for 'operator=' (operand types are 'std::tuple<int&, int&>' and 'std::tuple<int, int, int>')
   29 |             tie(c,d)=dfs(to, v);
      |                               ^
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68,
                 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/tuple:2355:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<const _U1&, const _U2&>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(const std::tuple<_U1, _U2>&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2355 |         operator=(const tuple<_U1, _U2>& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2355:9: note:   template argument deduction/substitution failed:
a.cc:29:31: note:   candidate expects 2 arguments, 3 provided
   29 |             tie(c,d)=dfs(to, v);
      |                               ^
/usr/include/c++/14/tuple:2365:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<_U1, _U2>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(std::tuple<_U1, _U2>&&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2365 |         operator=(tuple<_U1, _U2>&& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2365:9: note:   template argument deduction/substitution failed:
a.cc:29:31: note:   candidate expects 2 arguments, 3 provided
   29 |             tie(c,d)=dfs(to, v);
      |                               ^
/usr/include/c++/14/tuple:2375:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<const _U1&, const _U2&>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2375 |         operator=(const pair<_U1, _U2>& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2375:9: note:   template argument deduction/substitution failed:
a.cc:29:31: note:   'std::tuple<int, int, int>' is not derived from 'const std::pair<_T1, _T2>'
   29 |             tie(c,d)=dfs(to, v);
      |                               ^
/usr/include/c++/14/tuple:2386:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<_U1, _U2>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2386 |         operator=(pair<_U1, _U2>&& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2386:9: note:   template argument deduction/substitution failed:
a.cc:29:31: note:   'std::tuple<int, int, int>' is not derived from 'std::pair<_T1, _T2>'
   29 |             tie(c,d)=dfs(to, v);
      |                               ^
/usr/include/c++/14/tuple:2332:7: note: candidate: 'std::tuple<_T1, _T2>& std::tuple<_T1, _T2>::operator=(std::__conditional_t<((bool)__assignable<const _T1&, const _T2&>()), const std::tuple<_T1, _T2>&, const std::__nonesuch&>) [with _T1 = int&; _T2 = int&; std::__conditional_t<((bool)__assignable<const _T1&, const _T2&>()), const std::tuple<_T1, _T2>&, const std::__nonesuch&> = const std::tuple<int&, int&>&]'
 2332 |       operator=(__conditional_t<__assignable<const _T1&, const _T2&>(),
      |       ^~~~~~~~
/usr/include/c++/14/tuple:2334:52: note:   no known conversion for argument 1 from 'std::tuple<int, int, int>' to 'std::__conditional_t<true, const std::tuple<int&, int&>&, const std::__nonesuch&>' {aka 'const std::tuple<int&, int&>&'}
 2332 |       operator=(__conditional_t<__assignable<const _T1&, const _T2&>(),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 2333 |                                 const tuple&,
      |                                 ~~~~~~~~~~~~~       
 2334 |                                 const __nonesuch&> __in)
      |                                 ~~~~~~~~~~~~~~~~~~~^~~~
/usr/include/c++/14/tuple:2343:7: note: candidate: 'std::tuple<_T1, _T2>& std::tuple<_T1, _T2>::operator=(std::__conditional_t<((bool)__assignable<_T1, _T2>()), std::tuple<_T1, _T2>&&, std::__nonesuch&&>) [with _T1 = int&; _T2 = int&; std::__conditional_t<((bool)__assignable<_T1, _T2>()), std::tuple<_T1, _T2>&&, std::__nonesuch&&> = std::tuple<int&, int&>&&]'
 2343 |       operator=(__conditional_t<__assignable<_T1, _T2>(),
      |       ^~~~~~~~
/usr/include/c++/14/tuple:2345:47: note:   no known conversion for argument 1 from 'std::tuple<int, int, int>' to 'std::__conditional_t<true, std::tuple<int&, int&>&&, std::__nonesuch&&>' {aka 'std::tuple<int&, int&>&&'}
 2343 |       operator=(__conditional_t<__assignable<_T1, _T2>(),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 2344 |                                 tuple&&,
      |                                 ~~~~~~~~       
 2345 |                                 __nonesuch&&> __in)
      |                                 ~~~~~~~~~~~~~~^~~~
a.cc:33:16: error: 'P' was not declared in this scope
   33 |         return P(children,dist);
      |                ^
a.cc: In function 'int main()':
a.cc:23:32: error: conversion from 'main()::<lambda(int, int)>' to non-scalar type 'std::function<std::tuple<int, int, int>(int, int)>' requested
   23 |     function<T(int,int)> dfs = [&](int v,int prev){
      |                                ^~~~~~~~~~~~~~~~~~~~
   24 |         int children=0,dist=0;
      |         ~~~~~~~~~~~~~~~~~~~~~~  
   25 |         if(S[v]=='1') children++;
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~
   26 |         for(auto to:G[v]){
      |         ~~~~~~~~~~~~~~~~~~      
   27 |             if(to==prev) continue;
      |             ~~~~~~~~~~~~~~~~~~~~~~
   28 |             int c,d;
      |             ~~~~~~~~            
   29 |             tie(c,d)=dfs(to, v);
      |             ~~~~~~~~~~~~~~~~~~~~
   30 |             children+=c;
      |             ~~~~~~~~~~~~        
   31 |             dist+=c+d;
      |             ~~~~~~~~~~          
   32 |         }
      |         ~                       
   33 |         return P(children,dist);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~
   34 |     };
      |     ~                           
a.cc:40:33: error: no match for 'operator=' (operand types are 'std::tuple<int&, int&>' and 'std::tuple<int, int, int>')
   40 |             tie(c,d)=dfs(to,root);
      |                                 ^
/usr/include/c++/14/tuple:2355:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<const _U1&, const _U2&>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(const std::tuple<_U1, _U2>&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2355 |         operator=(const tuple<_U1, _U2>& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2355:9: note:   template argument deduction/substitution failed:
a.cc:40:33: note:   candidate expects 2 arguments, 3 provided
   40 |             tie(c,d)=dfs(to,root);
      |                                 ^
/usr/include/c++/14/tuple:2365:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<_U1, _U2>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(std::tuple<_U1, _U2>&&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2365 |         operator=(tuple<_U1, _U2>&& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2365:9: note:   template argument deduction/substitution failed:
a.cc:40:33: note:   candidate expects 2 arguments, 3 provided
   40 |             tie(c,d)=dfs(to,root);
      |                                 ^
/usr/include/c++/14/tuple:2375:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<const _U1&, const _U2&>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2375 |         operator=(const pair<_U1, _U2>& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2375:9: note:   template argument deduction/substitution failed:
a.cc:40:33: note:   'std::tuple<int, int, int>' is not derived from 'const std::pair<_T1, _T2>'
   40 |             tie(c,d)=dfs(to,root);
      |                                 ^
/usr/include/c++/14/tuple:2386:9: note: candidate: 'template<class _U1, class _U2> std::__enable_if_t<((bool)__assignable<_U1, _U2>()), std::tuple<_T1, _T2>&> std::tuple<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U2 = _U1; _T1 = int&; _T2 = int&]'
 2386 |         operator=(pair<_U1, _U2>&& __in)
      |         ^~~~~~~~
/usr/include/c++/14/tuple:2386:9: note:   template argument deduction/substitution failed:
a.cc:40:33: note:   'std::tuple<int, int, int>' is not derived from 'std::pair<_T1, _T2>'
   40 |             tie(c,d)=dfs(to,root);
      |                                 ^
/usr/include/c++/14/tuple:2332:7: note: candidate: 'std::tuple<_T1, _T2>& std::tuple<_T1, _T2>::operator=(std::__conditional_t<((bool)__assignable<const _T1&, const _T2&>()), const std::tuple<_T1, _T2>&, const std::__nonesuch&>) [with _T1 = int&; _T2 = int&; std::__conditional_t<((bool)__assignable<const _T1&, const _T2&>()), const std::tuple<_T1, _T2>&, const std::__nonesuch&> = const std::tuple<int&, int&>&]'
 2332 |       operator=(__conditional_t<__assignable<const _T1&, const _T2&>(),
      |       ^~~~~~~~
/usr/include/c++/14/tuple:2334:52: note:   no known conversion for argument 1 from 'std::tuple<int, int, int>' to 'std::__conditional_t<true, const std::tuple<int&, int&>&, const std::__nonesuch&>' {aka 'const std::tuple<int&, int&>&'}
 2332 |       operator=(__conditional_t<__assignable<const _T1&, const _T2&>(),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 2333 |                                 const tuple&,
      |                                 ~~~~~~~~~~~~~       
 2334 |                                 const __nonesuch&> __in)
      |                                 ~~~~~~~~~~~~~~~~~~~^~~~
/usr/include/c++/14/tuple:2