Problem p03442 (C++)

Buggy submission (status: Runtime Error):
// 20
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
template <class I> inline void read(I &x) {
  int f = 1;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-')
      f = -1;
  for (x = 0; c >= '0' && c <= '9';
       x = (x << 3) + (x << 1) + (c & 15), c = getchar())
    ;
  x *= f;
}
int n;
int tot = 0, num[N], cnt[32], p[33], f[(1 << 16) + 7], ans;
inline bool pd(int x, int w) {
  return (x & w) == w;
} /*
 bool dfs(int x,int fa,int w){
         int res=0;
         for(int i=head[x];i;i=nxt[i]){
                 int y=ver[i];
                 if(y==fa) continue;
                 if(!pd(edge[i],w))ans[w]+=dfs(y,x,w);
                 else dfs(y,x,w),res+=1;
         }
         ans[w]+=res/2;
         if(x==1) ans[w]+=res&1;
         return res&1;
 }*/
inline bool ch(int x, int y) {
  int res = x + y;
  if ((res ^ y) == x)
    return 0;
  return 1;
}
int g(int x) {
  int now = 0, res = 0, sz = 0;
  while (x) {
    if (x & 1)
      res ^= p[now], sz++;
    x >>= 1;
    now++;
  }
  return res ? sz : sz - 1;
}
int main() {
  // freopen("xor.in","r",stdin);
  // freopen("xor.out","w",stdout);
  read(n);
  for (int i = 1; i < n; i++) {
    int x, y, z;
    read(x), read(y), read(z);
    num[x] ^= z, num[y] ^= z;
  }
  for (int i = 0; i < n; i++)
    cnt[num[i]]++;
  for (int i = 1; i <= 31; i++) {
    ans += cnt[i] / 2;
    if (cnt[i] & 1)
      p[tot++] = i;
  }
  for (int i = 1; i < 1 << 16; i++) {
    f[i] = g(i);
    for (int j = i; j; j = (j - 1) & i) {
      f[i] = min(f[i], f[j] + f[i ^ j]);
    }
  }
  printf("%d\n", ans + f[(1 << (tot - 1)) - 1]);
  return 0;
}

Fixed submission:
// 20
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
template <class I> inline void read(I &x) {
  int f = 1;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-')
      f = -1;
  for (x = 0; c >= '0' && c <= '9';
       x = (x << 3) + (x << 1) + (c & 15), c = getchar())
    ;
  x *= f;
}
int n;
int tot = 0, num[N], cnt[32], p[33], f[(1 << 16) + 7], ans;
inline bool pd(int x, int w) {
  return (x & w) == w;
} /*
 bool dfs(int x,int fa,int w){
         int res=0;
         for(int i=head[x];i;i=nxt[i]){
                 int y=ver[i];
                 if(y==fa) continue;
                 if(!pd(edge[i],w))ans[w]+=dfs(y,x,w);
                 else dfs(y,x,w),res+=1;
         }
         ans[w]+=res/2;
         if(x==1) ans[w]+=res&1;
         return res&1;
 }*/
inline bool ch(int x, int y) {
  int res = x + y;
  if ((res ^ y) == x)
    return 0;
  return 1;
}
int g(int x) {
  int now = 0, res = 0, sz = 0;
  while (x) {
    if (x & 1)
      res ^= p[now], sz++;
    x >>= 1;
    now++;
  }
  return res ? sz : sz - 1;
}
int main() {
  // freopen("xor.in","r",stdin);
  // freopen("xor.out","w",stdout);
  read(n);
  for (int i = 1; i < n; i++) {
    int x, y, z;
    read(x), read(y), read(z);
    num[x] ^= z, num[y] ^= z;
  }
  for (int i = 0; i < n; i++)
    cnt[num[i]]++;
  for (int i = 1; i <= 31; i++) {
    ans += cnt[i] / 2;
    if (cnt[i] & 1)
      p[tot++] = i;
  }
  for (int i = 1; i < 1 << 16; i++) {
    f[i] = g(i);
    for (int j = i; j; j = (j - 1) & i) {
      f[i] = min(f[i], f[j] + f[i ^ j]);
    }
  }
  if (tot)
    printf("%d\n", ans + f[(1 << (tot - 1)) - 1]);
  else
    printf("%d\n", ans);
  return 0;
}