atcoder#APC001F. XOR Tree

XOR Tree

Score : 10001000 points

Problem Statement

You are given a tree with NN vertices. The vertices are numbered 00 through N1N-1, and the edges are numbered 11 through N1N-1. Edge ii connects Vertex xix_i and yiy_i, and has a value aia_i. You can perform the following operation any number of times:

  • Choose a simple path and a non-negative integer xx, then for each edge ee that belongs to the path, change aea_e by executing aeaexa_e \gets a_e \oplus x (⊕ denotes XOR).

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

Constraints

  • 2N1052 \leq N \leq 10^5
  • 0xi,yiN10 \leq x_i,y_i \leq N-1
  • 0ai150 \leq a_i \leq 15
  • The given graph is a tree.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

NN

x1x_1 y1y_1 a1a_1

x2x_2 y2y_2 a2a_2

::

xN1x_{N-1} yN1y_{N-1} aN1a_{N-1}

Output

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

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

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

  • First, choose the path connecting Vertex 1,21, 2, and x=1x = 1.
  • Then, choose the path connecting Vertex 2,32, 3, and x=2x = 2.
  • Lastly, choose the path connecting Vertex 0,40, 4, and x=4x = 4.
2
1 0 0
0