Language: C++

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define rng() (rand() >> 3)*rand()
#define scan(X) do{while((X=getchar())<'0'); for(X-='0'; '0'<=(_=getchar()); X=(X<<3)+(X<<1)+_-'0');}while(0)
char _;
#define pi 3.14159265358979323846

int n, m, a, b, w, x[100005], c[16], ans, p3[20];
short dp[15000000];
vector<int> r;
inline bool val(int B){
    int C=0;
    bool f=0;
    fox(l, m){
        if (B/p3[l]%3==1){
            C^=r[l];
            f=1;
        }
    }
    return f && C==0;
}
inline int nxt(int B){
    fox(l, m){
        if (B/p3[l]%3==1) B+=p3[l];
    }
    return B;
}
int main(){
    p3[0]=1; fox1(l, 18) p3[l]=p3[l-1]*3;
    scanf("%i", &n); ans=n;
    fox(l, n-1){
        scanf("%i%i%i", &a, &b, &w);
        x[a]^=w;
        x[b]^=w;
    }
    fox(l, n){
        c[x[l]]++;
        //cout << x[l] << ' ';
    }
    ans-=c[0];
    fox1(l, 15){
        ans-=c[l]/2;
        c[l]%=2;
        if (c[l]) r.pb(l);
    }
    if (r.size()){
        fox(l, p3[m]) dp[l]=-(1<<30);
        dp[0]=0;
        m=r.size();
        fox(l, p3[m]){
            if (dp[l]<0) continue;
            fox(l2, m){
                if (l/p3[l2]%3==0){
                    dp[l+p3[l2]]=max(dp[l+p3[l2]], dp[l]);
                }
            }
            if (val(l)){
                //cout << "*";
                dp[nxt(l)]=max(dp[nxt(l)], dp[l]+1);
            }
            //cout << l << ' ' << dp[l] << endl;
        }
        ans-=dp[p3[m]-1];
    }
    cout << ans;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:69:29: warning: overflow in conversion from 'int' to 'short int' changes value from '-1073741824' to '0' [-Woverflow]
   69 |         fox(l, p3[m]) dp[l]=-(1<<30);
      |                             ^~~~~~~~
a.cc:81:31: error: no matching function for call to 'max(short int&, int)'
   81 |                 dp[nxt(l)]=max(dp[nxt(l)], dp[l]+1);
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:81:31: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   81 |                 dp[nxt(l)]=max(dp[nxt(l)], dp[l]+1);
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:81:31: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   81 |                 dp[nxt(l)]=max(dp[nxt(l)], dp[l]+1);
      |                            ~~~^~~~~~~~~~~~~~~~~~~~~