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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

// package agc.apc001;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        int[] xor = new int[n];
        for (int i = 0; i < n-1 ; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            int a = in.nextInt();
            xor[x] ^= a;
            xor[y] ^= a;
        }
        int[] px = new int[16];
        for (int i = 0; i < n ; i++) {
            px[xor[i]]++;
        }

        int cost = 0;
        int left = 0;
        for (int i = 1 ; i < px.length; i++) {
            cost += px[i] / 2;
            if (px[i] % 2 == 1) {
                left ^= 1<<(i-1);
            }
        }

        int[] dp = new int[1<<15];
        Arrays.fill(dp, 127);
        dp[left] = 0;

        Queue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(o -> o[0]));
        q.add(new int[]{0, left});
        while (q.size() >= 1) {
            int[] qt = q.poll();
            int now = qt[1];
            int time = qt[0];
            if (dp[now] < time) {
                continue;
            }

            for (int i = 0; i < 15; i++) {
                if ((now & (1<<i)) == 0) {
                    continue;
                }
                for (int j = i+1 ; j < 15; j++) {
                    if ((now & (1<<j)) == 0) {
                        continue;
                    }
                    for (int k = 1 ; k <= 15; k++) {
                        int tc = time+1;
                        int nw = now ^ (1<<i) ^ (1<<j);
                        int ti = ((i+1) ^ k);
                        int tj = ((j+1) ^ k);
                        if (ti >= 1) {
                            nw ^= (1<<(ti-1));
                            if ((nw & (1<<(ti-1))) == 0) {
                                tc++;
                            }
                        }
                        if (tj >= 1) {
                            nw ^= (1<<(tj-1));
                            if ((nw & (1<<(tj-1))) == 0) {
                                tc++;
                            }
                        }
                        if (dp[nw] > tc) {
                            dp[nw] = tc;
                            q.add(new int[]{tc, nw});
                        }
                    }
                }
            }
        }
        out.println(cost + dp[0]);
        out.flush();
    }

    public static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }

    public static class InputReader {
        private static final int BUFFER_LENGTH = 1 << 12;
        private InputStream stream;
        private byte[] buf = new byte[BUFFER_LENGTH];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int next() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public char nextChar() {
            return (char) skipWhileSpace();
        }

        public String nextToken() {
            int c = skipWhileSpace();
            StringBuilder res = new StringBuilder();
            do {
                res.append((char) c);
                c = next();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public int nextInt() {
            return (int) nextLong();
        }

        public long nextLong() {
            int c = skipWhileSpace();
            long sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble() {
            return Double.valueOf(nextToken());
        }

        int skipWhileSpace() {
            int c = next();
            while (isSpaceChar(c)) {
                c = next();
            }
            return c;
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }
}