#P6072. 『MdOI R1』Path
『MdOI R1』Path
Description
You are given an undirected tree with nodes, and each edge has a weight.
Let and denote the set of all nodes and the set of all edges on the simple path between and in the tree. In particular, when , and .
Define the weight of an edge set as the bitwise XOR sum of the weights of all edges in . When , .
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 , the number of nodes in the tree.
The next lines each contain three integers , indicating that there is an edge of weight between nodes and .
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 (where 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 . Note that the red path cannot be extended to , otherwise the blue path would not be possible.

[Constraints]
This problem uses bundled testdata.
| Subtask ID | Special Property | Score | Time Limit | |
|---|---|---|---|---|
| 1 | None | 12 | 1s | |
| 2 | 28 | 2s | ||
| 3 | 20 | 3s | ||
| 4 | None | 40 | 3.5s |
For of the testdata, , , .
Input Format
The first line contains an integer , the number of nodes in the tree.
The next lines each contain three integers , indicating that there is an edge of weight between nodes and .
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 (where 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 . Note that the red path cannot be extended to , otherwise the blue path would not be possible.

[Constraints]
This problem uses bundled testdata.
| Subtask ID | Special Property | Score | Time Limit | |
|---|---|---|---|---|
| 1 | None | 12 | 1s | |
| 2 | 28 | 2s | ||
| 3 | 20 | 3s | ||
| 4 | None | 40 | 3.5s |
For of the testdata, , , 。
Translated by ChatGPT 5
京公网安备 11011102002149号