Problem p03442 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define Tp template <typename Ty>
#define Ts template <typename Ty, typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int &
#define I inline
#define W while
#define N 100000
#define P 15
#define INF 1e9
#define Gmin(x, y) (x > (y) && (x = (y)))
const int maxn = 100035;
const int maxs = 1 << 17;

int n, cnt, a[maxn], f[maxs], sta, ans;

class Class_FIO {
private:
#define FS 100000
#define tc()                                                                   \
  (A == B && (B = (A = FI) + fread(FI, 1, FS, stdin), A == B) ? EOF : *A++)
#define pc(c) (putchar(c))
#define tn(x) (x << 3) + (x << 1)
#define D isdigit(c = tc())
  int T;
  char c, *A, *B, FI[FS], S[FS];

public:
  I Class_FIO() { A = B = FI; }
  Tp I void read(Ty &x) {
    x = 0;
    W(!D);
    W(x = tn(x) + (c & 15), D);
  }
  Tp I void write(Ty x) {
    W(S[++T] = x % 10 + 48, x /= 10);
    W(T) pc(S[T--]);
  }
  Ts I void read(Ty &x, Ar &...y) { read(x), read(y...); }
} F;
int dp(int x) {
  if (!x)
    return 0;
  if (f[x] != -1)
    return f[x];
  int ret = 1e9;
  for (int i = 1; i < 16; i++)
    if (x & (1 << i))
      for (int j = 1; j < 16; j++)
        if ((x & (1 << j)) && i != j)
          ret = std::min(ret, dp(x ^ (1 << i) ^ (1 << j) ^ (1 << (i ^ j))) + 1 +
                                  ((x & (1 << (i ^ j))) ? 1 : 0));
  f[x] = ret;
  return ret;
}
int main() {
  freopen("xor.in", "r", stdin);
  freopen("xor.out", "w", stdout);
  F.read(n);
  memset(f, -1, sizeof f);
  for (int i = 1; i < n; i++) {
    int x, y, z;
    F.read(x), ++x, F.read(y), ++y, F.read(z);
    a[x] ^= z, a[y] ^= z;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] && ((sta >> a[i]) & 1))
      sta ^= 1 << a[i], ++ans;
    else if (a[i])
      sta ^= 1 << a[i];
  printf("%d\n", ans + dp(sta));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define Tp template <typename Ty>
#define Ts template <typename Ty, typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int &
#define I inline
#define W while
#define N 100000
#define P 15
#define INF 1e9
#define Gmin(x, y) (x > (y) && (x = (y)))
const int maxn = 100035;
const int maxs = 1 << 17;

int n, cnt, a[maxn], f[maxs], sta, ans;

class Class_FIO {
private:
#define FS 100000
#define tc()                                                                   \
  (A == B && (B = (A = FI) + fread(FI, 1, FS, stdin), A == B) ? EOF : *A++)
#define pc(c) (putchar(c))
#define tn(x) (x << 3) + (x << 1)
#define D isdigit(c = tc())
  int T;
  char c, *A, *B, FI[FS], S[FS];

public:
  I Class_FIO() { A = B = FI; }
  Tp I void read(Ty &x) {
    x = 0;
    W(!D);
    W(x = tn(x) + (c & 15), D);
  }
  Tp I void write(Ty x) {
    W(S[++T] = x % 10 + 48, x /= 10);
    W(T) pc(S[T--]);
  }
  Ts I void read(Ty &x, Ar &...y) { read(x), read(y...); }
} F;
int dp(int x) {
  if (!x)
    return 0;
  if (f[x] != -1)
    return f[x];
  int ret = 1e9;
  for (int i = 1; i < 16; i++)
    if (x & (1 << i))
      for (int j = 1; j < 16; j++)
        if ((x & (1 << j)) && i != j)
          ret = std::min(ret, dp(x ^ (1 << i) ^ (1 << j) ^ (1 << (i ^ j))) + 1 +
                                  ((x & (1 << (i ^ j))) ? 1 : 0));
  f[x] = ret;
  return ret;
}
int main() {
  F.read(n);
  memset(f, -1, sizeof f);
  for (int i = 1; i < n; i++) {
    int x, y, z;
    F.read(x), ++x, F.read(y), ++y, F.read(z);
    a[x] ^= z, a[y] ^= z;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] && ((sta >> a[i]) & 1))
      sta ^= 1 << a[i], ++ans;
    else if (a[i])
      sta ^= 1 << a[i];
  printf("%d\n", ans + dp(sta));
  return 0;
}