#P4151. [WC2011] 最大XOR和路径

    ID: 3087 远端评测题 1000ms 500MiB 尝试: 1 已通过: 0 难度: 8 上传者: 标签>图论贪心2011WC/CTSC/集训队枚举,暴力深度优先搜索,DFS线性基向量

[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 (11 means true, 00 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 1212 XOR 99 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, 1212 XOR 9=59 = 5.

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 KK non-negative integers A1A_1, A2A_2, …, AK1A_{K-1}, AKA_K as

A1A_1 XOR A2A_2 XOR … XOR AK1A_{K-1} XOR AKA_K.

Consider an undirected connected graph with non-negative integer edge weights, with nodes numbered from 11 to NN. Find a path from node 11 to node NN 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 NN and MM, the number of nodes and edges in the undirected graph.

The next MM lines each describe an edge, with three integers SiS_i, TiT_i, DiD_i, indicating there is an undirected edge between SiS_i and TiT_i with weight DiD_i.

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

22 XOR 11 XOR 22 XOR 44 XOR 11 XOR 11 XOR 3=63 = 6.

Of course, a shorter path 1351 \rightarrow 3 \rightarrow 5 also has XOR sum 22 XOR 4=64 = 6.

Constraints

  • For 20% of the testdata, N100N \leq 100, M1000M \leq 1000, Di104D_i \leq 10^{4}.
  • For 50% of the testdata, N1000N \leq 1000, M10000M \leq 10000, Di1018D_i \leq 10^{18}.
  • For 70% of the testdata, N5000N \leq 5000, M50000M \leq 50000, Di1018D_i \leq 10^{18}.
  • For 100% of the testdata, N50000N \leq 50000, M100000M \leq 100000, Di1018D_i \leq 10^{18}.

Translated by ChatGPT 5