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] = i;
            //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] = i - (1 << j) - (1 << k) - (1 << l);
                    dec[i] = dec[dp[i]] + 1;
                    break;
                }
                if (dp[i] != i) break;
            }
            if (dp[i] != i) continue;
            //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] = i - (1 << j) - (1 << k) - (1 << l) - (1 << m);
                        dec[i] = dec[dp[i]] + 1;
                        break;
                    }
                    if (dp[i] != i) break;
                }
                if (dp[i] != i) break;
            }

            //calc-next
            if (dp[i] == i)
            {
                for (int j = 0; j < B; j++)
                {
                    next[i, j] = i | (1 << j);
                }

                for (int j = 0; j < B; j++)
                {
                    if (((i >> j) & 1) == 0) continue;
                    nextok[i, j] = true;
                    next[i, j] = (1 << j);

                    for (int k = j + 1; k < B; k++)
                    {
                        if (((i >> k) & 1) == 0) continue;

                        nextok[i, j ^ k] = true;
                        next[i, j ^ k] = (1 << j) | (1 << k);

                        for (int l = k + 1; l < B; l++)
                        {
                            if (((i >> l) & 1) == 0) continue;

                            nextok[i, j ^ k ^ l] = true;
                            next[i, j ^ k ^ l] = (1 << j) | (1 << k) | (1 << l);

                            for (int m = l + 1; m < B; m++)
                            {
                                if (((i >> m) & 1) == 0) continue;
                                nextok[i, j ^ k ^ l ^ m] = true;
                                next[i, j ^ k ^ l ^ m] = (1 << j) | (1 << k) | (1 << l) | (1 << m);
                            }
                        }
                        if (dp[i] != i) break;
                    }
                    if (dp[i] != i) break;
                }
            }
        }

        N = cin.nextInt();
        X = new int[N];
        Y = new int[N];
        A = 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();
            es[X[i]].Add(Tuple.Create(Y[i], A[i]));
            es[Y[i]].Add(Tuple.Create(X[i], A[i]));
        }


        dfs(0, -1, 0);
        Console.WriteLine(ans);
    }

    int dfs(int a, int parent, int preMove)
    {
        int flag = 0;
        foreach (var item in es[a])
        {
            int c = item.Item1;
            int f = item.Item2;
            if (c == parent) continue;
            int ff = dfs(c, a, f);
            int and = ff & flag;
            ans -= bitCount(and);
            flag ^= ff;
        }
        ans -= dec[flag];
        flag = dp[flag];

        if (preMove == 0) return 0;
        int nextflag = next[flag, preMove];
        if(!nextok[flag, preMove])
        {
            ans++;
        }
        return nextflag;
    }

    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;
    }
}