F - XOR Tree Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 10001000

問題文

NN 頂点の木が与えられます。頂点には 00 から N1N-1 の番号がついています。 辺は 11 から N1N-1 までの番号がついていて、辺 ii は頂点 xix_iyiy_i をつなぎ、また aia_i という値を保持しています。 あなたは以下の操作を何回でもすることが出来ます:

  • ある単純pathとある非負整数 xx を選び、そのpathを構成する各辺 ee について、 aeaexa_e ← a_e ⊕ x (⊕ は xor)と変化させる。

目標はすべての辺 ee について ae=0a_e = 0 とすることです。 目標を達成するために必要な最小の操作回数を求めてください。

制約

  • 2N1052 ≤ N ≤ 10^5
  • 0xi,yiN10 ≤ x_i,y_i ≤ N-1
  • 0ai150 ≤ a_i ≤ 15
  • 与えられるグラフは木
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN
x1x_1 y1y_1 a1a_1
x2x_2 y2y_2 a2a_2
::
xN1x_{N-1} yN1y_{N-1} aN1a_{N-1}

出力

目標を達成するために必要な操作回数の最小値を出力せよ。


入力例 1Copy

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

出力例 1Copy

Copy
3

以下のようにして三回で目標を達成できます。

  • まず、頂点 1,21,2 を結ぶ path に対して x=1x=1 で操作する
  • 次に、頂点 2,32,3 を結ぶ path に対して x=2x=2 で操作する
  • 最後に、頂点 0,40,4 を結ぶ path に対して x=4x=4 で操作する

入力例 2Copy

Copy
2
1 0 0

出力例 2Copy

Copy
0

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 ← a_e ⊕ 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 ≤ N ≤ 10^5
  • 0xi,yiN10 ≤ x_i,y_i ≤ N-1
  • 0ai150 ≤ 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:

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.


Sample Input 1Copy

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

Sample Output 1Copy

Copy
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.

Sample Input 2Copy

Copy
2
1 0 0

Sample Output 2Copy

Copy
0


2025-03-13 (Thu)
20:30:26 +00:00