Problem:
Score : 1000 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 0 through N-1, and the edges are numbered 1 through N-1.
Edge i connects Vertex x_i and y_i, and has a value a_i.
You can perform the following operation any number of times:

Choose a simple path and a non-negative integer x, then for each edge e that belongs to the path, change a_e by executing a_e ← a_e ⊕ x (⊕ denotes XOR).

Your objective is to have a_e = 0 for all edges e.
Find the minimum number of operations required to achieve it.

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ N-1

0 ≤ a_i ≤ 15

The given graph is a tree.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_{N-1} y_{N-1} a_{N-1}

Output

Find the minimum number of operations required to achieve the objective.

Sample Input 1

5
0 1 1
0 2 3
0 3 6
3 4 4

Sample Output 1

3

The objective can be achieved in three operations, as follows:

First, choose the path connecting Vertex 1, 2, and x = 1.

Then, choose the path connecting Vertex 2, 3, and x = 2.

Lastly, choose the path connecting Vertex 0, 4, and x = 4.

Sample Input 2

2
1 0 0

Sample Output 2

0

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <cstdio>
#include <vector>
using namespace std;
int a[100010];
int c[16];
int dp[1<<16];
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n - 1; i++) {
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		a[x] ^= z;
		a[y] ^= z;
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		c[a[i]]++;
	}
	vector<int> v;
	for (int i = 1; i < 16; i++) {
		ans += c[i] / 2;
		if (c[i] & 1) {
		       v.push_back(i);
		}
	}
	int m = v.size();
	dp[0] = 0;
	for (int i = 1; i < 1 << m; i++) {
		int k = 0, x = 0;
		for (int j = 0; j < m; j++) {
			if (i >> j & 1) {
				x ^= v[j];
				k++;
			}
		}
		if (x) {
			dp[i] = 100;
		} else {
			dp[i] = k - 1;
		}
		for (int j = i; j; j = i & (j - 1)) {
			if (dp[i] > dp[j] + dp[i^j]) {
				dp[i] = dp[j] + dp[i^j];
			}
		}
	}
	printf("%d\n", ans + dp[(1<<m)-1]);
	return 0;
}