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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;



class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }


    Scanner cin;


    int N;
    int[] X;
    int[] Y;
    int[] A;
    List<Tuple<int, int>>[] es;

    const int MAX = (1 << 16);
    const int B = 16;
    int[] dp = new int[MAX];
    int[] dec = new int[MAX];
    int[,] next = new int[MAX, 16];
    bool[,] nextok = new bool[MAX, 16];

    int ans = 0;
    

    void calc()
    {

        cin = new Scanner();

        for (int i = 0; i < MAX; i++)
        {
            dp[i] = 0;
            //3-set
            for (int j = 0; j < B; j++)
            {
                if (((i >> j) & 1) == 0) continue;
                for (int k = j + 1; k < B; k++)
                {
                    if (((i >> k) & 1) == 0) continue;
                    int l = j ^ k;
                    if (((i >> l) & 1) == 0) continue;
                    if (l <= k) continue;
                    dp[i] = Math.Max(dp[i], dp[i - (1 << j) - (1 << k) - (1 << l)] + 1);
                }
            }
            //4-set
            for (int j = 0; j < B; j++)
            {
                if (((i >> j) & 1) == 0) continue;
                for (int k = j + 1; k < B; k++)
                {
                    if (((i >> k) & 1) == 0) continue;
                    for (int l = k + 1; l < B; l++)
                    {
                        if (((i >> l) & 1) == 0) continue;

                        int m = (j ^ k ^ l);
                        if (((i >> m) & 1) == 0) continue;
                        if (m <= l) continue;
                        dp[i] = Math.Max(dp[i], dp[i - (1 << j) - (1 << k) - (1 << l) - (1 << m)] + 1);
                    }
                }
            }
        }

        N = cin.nextInt();
        X = new int[N];
        Y = new int[N];
        A = new int[N];
        int[] P = new int[N];
        es = new List<Tuple<int, int>>[N];

        for (int i = 0; i < N; i++)
        {
            es[i] = new List<Tuple<int, int>>();
        }
        
        for (int i = 0; i < N - 1; i++)
        {
            X[i] = cin.nextInt();
            Y[i] = cin.nextInt();
            A[i] = cin.nextInt();
            P[X[i]] ^= A[i];
            P[Y[i]] ^= A[i];
        }

        int[] sum = new int[B];
        for (int i = 0; i < N; i++)
        {
            sum[P[i]]++;
        }

        int ans = 0;
        int flag = 0;
        for (int i = 1; i < B; i++)
        {
            ans += (sum[i] + 1) / 2;
            if (sum[i] % 2 == 1) flag |= (1 << i);
        }
        
        ans -= dp[flag];
        Console.WriteLine(ans);
    }

    int bitCount(long x)
    {
        x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
        x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
        x = (x & 0x0f0f0f0f0f0f0f0f) + (x >> 4 & 0x0f0f0f0f0f0f0f0f);
        x = (x & 0x00ff00ff00ff00ff) + (x >> 8 & 0x00ff00ff00ff00ff);
        x = (x & 0x0000ffff0000ffff) + (x >> 16 & 0x0000ffff0000ffff);
        return (int)((x & 0x00000000ffffffff) + (x >> 32 & 0x00000000ffffffff));
    }


}


class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}