#P6072. 『MdOI R1』Path

    ID: 4842 远端评测题 1000~3500ms 245MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>莫队O2优化分治字典树,Trie 树

『MdOI R1』Path

Description

You are given an undirected tree with nn nodes, and each edge has a weight.

Let V(x,y)V(x,y) and E(x,y)E(x,y) denote the set of all nodes and the set of all edges on the simple path between xx and yy in the tree. In particular, when x=yx=y, V(x,y)={x}V(x,y)=\{x\} and E(x,y)=E(x,y)=\varnothing.

Define the weight f(E)f(E) of an edge set EE as the bitwise XOR sum of the weights of all edges in EE. When E=E=\varnothing, f(E)=0f(E)=0.

Now, you need to compute

$$\max_{1\le x,y,u,v \le n,V(x,y)\cap V(u,v) = \varnothing}(f(E(x,y)) + f(E(u,v)))$$

In plain words, you need to choose two simple paths such that they do not share any common nodes, and the sum of their edge-weight XOR values is as large as possible.

Input Format

The first line contains an integer nn, the number of nodes in the tree.

The next n1n-1 lines each contain three integers x,y,wx,y,w, indicating that there is an edge of weight ww between nodes xx and yy.

Output Format

Output one integer on a single line, the answer.

9
1 2 1
1 3 7
2 4 8
3 5 3
4 6 3
3 7 3
7 8 5
7 9 2

21

3
1 2 2
2 3 1

2

Hint

[Sample 1 Explanation]

The tree in the sample is shown in the figure. Choose the two paths marked in red and blue. They have no common nodes, and the sum of XOR values is maximized, which is (718)+(52)=21(7\oplus 1\oplus 8)+(5\oplus 2)=21 (where \oplus denotes the XOR operation).

[Sample 2 Explanation]

The tree in the sample is shown in the figure. It is a chain. Choose the red and blue paths; the blue path degenerates into a single node, making the maximum sum of XOR values 2+0=22+0=2. Note that the red path cannot be extended to 33, otherwise the blue path would not be possible.


[Constraints]

This problem uses bundled testdata.

Subtask ID nn\leq Special Property Score Time Limit
1 5050 None 12 1s
2 2×1032\times 10^3 28 2s
3 2×1042\times 10^4 y=x+1y = x + 1 20 3s
4 3×1043\times 10^4 None 40 3.5s

For 100%100\% of the testdata, 2n3×1042\leq n\leq 3\times 10^4, 1x,yn1\leq x,y\leq n, 0w1090\leq w\leq 10^9.

Input Format

The first line contains an integer nn, the number of nodes in the tree.

The next n1n-1 lines each contain three integers x,y,wx,y,w, indicating that there is an edge of weight ww between nodes xx and yy.

Output Format

Output one integer on a single line, the answer.

Hint

[Sample 1 Explanation]

The tree in the sample is shown in the figure. Choose the two paths marked in red and blue. They have no common nodes, and the sum of XOR values is maximized, which is (718)+(52)=21(7\oplus 1\oplus 8)+(5\oplus 2)=21 (where \oplus denotes the XOR operation).

[Sample 2 Explanation]

The tree in the sample is shown in the figure. It is a chain. Choose the red and blue paths; the blue path degenerates into a single node, making the maximum sum of XOR values 2+0=22+0=2. Note that the red path cannot be extended to 33, otherwise the blue path would not be possible.


[Constraints]

This problem uses bundled testdata.

Subtask ID nn\leq Special Property Score Time Limit
1 5050 None 12 1s
2 2×1032\times 10^3 28 2s
3 2×1042\times 10^4 y=x+1y = x + 1 20 3s
4 3×1043\times 10^4 None 40 3.5s

For 100%100\% of the testdata, 2n3×1042\leq n\leq 3\times 10^4, 1x,yn1\leq x,y\leq n, 0w1090\leq w\leq 10^9

Translated by ChatGPT 5