Problem p03442 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>

#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <list>
#include <utility>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <unordered_map>
#include <unordered_set>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> l4;
typedef pair<double, double> dd;
typedef unsigned long long ull;
#define mp make_pair
#define pb push_back

#define debug(x) cerr << #x << " = " << x << " "

const int N = 1e5;
int val[N];
const int B = 4;
const int MB = 1 << B;
const int MMB = 1 << MB;
int cnt[MB];
int d[MMB];
int bitvalue[MMB];
int bitcnt[MMB];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int u, v, x;
    scanf("%d %d %d", &u, &v, &x);
    val[u] ^= x;
    val[v] ^= x;
  }
  for (int i = 0; i < n; ++i)
    ++cnt[val[i]];
  int ans = 0;
  for (int i = 0; i < MB; ++i)
    bitvalue[1 << i] = i;
  for (int i = 1; i < MB; ++i)
    ans += cnt[i] / 2, cnt[i] %= 2;
  for (int i = 1; i < MMB; ++i) {
    int lb = i & -i;
    bitvalue[i] = bitvalue[i ^ lb] ^ bitvalue[lb];
    bitcnt[i] = bitcnt[i >> 1] + (i & 1);
    d[i] = MB;
    for (int sub = i; sub; sub = (sub - 1) & i)
      if (bitvalue[sub] == 0)
        d[i] = min(d[i], bitcnt[sub] - 1 + d[i ^ sub]);
  }
  int mask = 0;
  for (int i = 1; i < n; ++i)
    mask ^= cnt[i] << i;
  printf("%d\n", d[mask] + ans);
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>

#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <list>
#include <utility>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <unordered_map>
#include <unordered_set>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> l4;
typedef pair<double, double> dd;
typedef unsigned long long ull;
#define mp make_pair
#define pb push_back

#define debug(x) cerr << #x << " = " << x << " "

const int N = 1e5;
int val[N];
const int B = 4;
const int MB = 1 << B;
const int MMB = 1 << MB;
int cnt[MB];
int d[MMB];
int bitvalue[MMB];
int bitcnt[MMB];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int u, v, x;
    scanf("%d %d %d", &u, &v, &x);
    val[u] ^= x;
    val[v] ^= x;
  }
  for (int i = 0; i < n; ++i)
    ++cnt[val[i]];
  int ans = 0;
  for (int i = 0; i < MB; ++i)
    bitvalue[1 << i] = i;
  for (int i = 1; i < MB; ++i)
    ans += cnt[i] / 2, cnt[i] %= 2;
  for (int i = 1; i < MMB; ++i) {
    int lb = i & -i;
    bitvalue[i] = bitvalue[i ^ lb] ^ bitvalue[lb];
    bitcnt[i] = bitcnt[i >> 1] + (i & 1);
    d[i] = MB;
    for (int sub = i; sub; sub = (sub - 1) & i)
      if (bitvalue[sub] == 0)
        d[i] = min(d[i], bitcnt[sub] - 1 + d[i ^ sub]);
  }
  int mask = 0;
  for (int i = 1; i < MB; ++i)
    mask ^= cnt[i] << i;
  printf("%d\n", d[mask] + ans);
}