#P4151. [WC2011] 最大XOR和路径
[WC2011] 最大XOR和路径
Description
XOR (exclusive OR) is a binary logical operation whose result is true if and only if the two input Boolean values are different; otherwise it is false. The truth table of the XOR operation is as follows ( means true, means false):
| Input | Output | |
|---|---|---|
| A | B | A XOR B |
| 0 | 0 | |
| 1 | 1 | |
| 1 | 0 | |
| 1 | 0 | |
The XOR of two non-negative integers means writing them in binary and applying XOR on the corresponding bits.
For example, the computation of XOR is as follows:
$$12=(1100)_2\ \ \ 9=(1001)_2\\ \begin{matrix} &1\ 1\ 0\ 0\\ \text{XOR}&1\ 0\ 0\ 1\\ \hline &0\ 1\ 0\ 1\\ \end{matrix}\\ (0101)_2=5$$Therefore, XOR .
It is easy to verify that XOR is commutative and associative, so the order does not affect the result when XOR-ing multiple numbers. Thus, we define the XOR sum of non-negative integers , , …, , as
XOR XOR … XOR XOR .
Consider an undirected connected graph with non-negative integer edge weights, with nodes numbered from to . Find a path from node to node such that the XOR sum of the edge weights along the path is maximized.
A path may visit some vertices or edges multiple times. When an edge appears multiple times in the path, its weight is counted the corresponding number of times in the XOR sum. See the sample for details.
Input Format
The first line contains two integers and , the number of nodes and edges in the undirected graph.
The next lines each describe an edge, with three integers , , , indicating there is an undirected edge between and with weight .
Multiple edges and self-loops may exist.
Output Format
Output a single integer, the maximum XOR sum (in decimal).
5 7
1 2 2
1 3 2
2 4 1
2 5 1
4 5 3
5 3 4
4 3 2
6
Hint
[Sample Explanation]

As shown, the path $1 \rightarrow 2 \rightarrow 4 \rightarrow 3 \rightarrow 5 \rightarrow 2 \rightarrow 4 \rightarrow 5$ has XOR sum
XOR XOR XOR XOR XOR XOR .
Of course, a shorter path also has XOR sum XOR .
Constraints
- For 20% of the testdata, , , .
- For 50% of the testdata, , , .
- For 70% of the testdata, , , .
- For 100% of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号