Problem p03442 (C++)

Buggy submission (status: Runtime Error):
///****In the name of Allah, most Gracious, most Compassionate****//

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

typedef long long ll;
typedef pair<int, int> pii;

#define ALL(a) a.begin(), a.end()
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr)
#define IN freopen("input.txt", "r+", stdin)
#define OUT freopen("output.txt", "w+", stdout)

#define DBG(a) cout << #a << " --> " << (a) << endl
#define NL cout << "\n"

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}

const int N = 3e5 + 5;
const int oo = 1e9 + 7;

int cnt[N];
int val[N];
int dp[1 << 16];
int rec(int mask) {
  if (mask == 0)
    return 0;
  if (dp[mask] != -1)
    return dp[mask];
  int &ret = dp[mask] = oo;
  for (int j = 1; j <= 15; j++) {
    if (mask & (1 << j) == 0)
      continue;
    for (int k = j + 1; k <= 15; k++) {
      if (mask & (1 << k)) {
        //                DBG(mask & (1<<k));
        int to = j ^ k;
        int tomask = mask ^ (1 << j) ^ (1 << k);

        bool jog = tomask & (1 << to);
        tomask = tomask ^ (1 << to);
        //                DBG(mask);
        //                DBG(tomask);
        //
        //                DBG(j);
        //                DBG(k);
        //                DBG(to);
        //                DBG(bitset<16> (mask));
        //                DBG(bitset<16> (tomask));
        //                NL;
        assert(__builtin_popcount(mask) > __builtin_popcount(tomask));
        ret = min(ret, 1 + rec(tomask) + jog);
      }
    }
  }
  return ret;
}
int32_t main() {
  FastIO;
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, a;
    cin >> u >> v >> a;
    val[u] ^= a;
    val[v] ^= a;
  }
  //    for(int i=0; i<n; i++) cout<<val[i]<<" ";
  //    NL;
  for (int i = 0; i < n; i++)
    cnt[val[i]]++;

  int ans = 0, mask = 0, jor = 0;
  for (int i = 1; i <= 15; i++) {
    ans += cnt[i] / 2;
    cnt[i] %= 2;
    if (cnt[i])
      mask |= (1 << i), jor ^= i;
  }
  memset(dp, -1, sizeof dp);
  ans += rec(mask);
  cout << ans << '\n';
}


Fixed submission:
///****In the name of Allah, most Gracious, most Compassionate****//

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

typedef long long ll;
typedef pair<int, int> pii;

#define ALL(a) a.begin(), a.end()
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr)
#define IN freopen("input.txt", "r+", stdin)
#define OUT freopen("output.txt", "w+", stdout)

#define DBG(a) cout << #a << " --> " << (a) << endl
#define NL cout << "\n"

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}

const int N = 3e5 + 5;
const int oo = 1e9 + 7;

int cnt[N];
int val[N];
int dp[1 << 16];
int rec(int mask) {
  if (mask == 0)
    return 0;
  if (dp[mask] != -1)
    return dp[mask];
  int &ret = dp[mask] = oo;
  for (int j = 1; j <= 15; j++) {
    if (mask & (1 << j) == 0)
      continue;
    for (int k = j + 1; k <= 15; k++) {
      if (mask & (1 << k)) {
        //                DBG(mask & (1<<k));
        int to = j ^ k;
        int tomask = mask ^ (1 << j) ^ (1 << k);

        bool jog = tomask & (1 << to);
        tomask = tomask ^ (1 << to);
        //                DBG(mask);
        //                DBG(tomask);
        //
        //                DBG(j);
        //                DBG(k);
        //                DBG(to);
        //                DBG(bitset<16> (mask));
        //                DBG(bitset<16> (tomask));
        //                NL;
        //                assert(__builtin_popcount(mask)>
        //                __builtin_popcount(tomask));
        ret = min(ret, 1 + rec(tomask) + jog);
      }
    }
  }
  return ret;
}
int32_t main() {
  FastIO;
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, a;
    cin >> u >> v >> a;
    val[u] ^= a;
    val[v] ^= a;
  }
  //    for(int i=0; i<n; i++) cout<<val[i]<<" ";
  //    NL;
  for (int i = 0; i < n; i++)
    cnt[val[i]]++;

  int ans = 0, mask = 0, jor = 0;
  for (int i = 1; i <= 15; i++) {
    ans += cnt[i] / 2;
    cnt[i] %= 2;
    if (cnt[i])
      mask |= (1 << i), jor ^= i;
  }
  memset(dp, -1, sizeof dp);
  ans += rec(mask);
  cout << ans << '\n';
}