#P3211. [HNOI2011] XOR和路径
[HNOI2011] XOR和路径
Description
Given an undirected connected graph with nodes labeled from to , each edge has a nonnegative integer weight. Find a path from node to node that maximizes the XOR sum of the weights of the edges along the path. The path may revisit nodes or edges. If an edge appears multiple times on the path, its weight is included in the XOR sum the corresponding number of times.
Directly solving the above problem is difficult, so you decide to use an imperfect algorithm. Specifically, starting from node , at each step you uniformly at random choose one edge incident to the current node and move along it to the next node. Repeat this process until you reach node to obtain a path from node to node . Clearly, different such paths occur with different probabilities, and each such path has a different XOR sum. Now compute the expected value of the XOR sum of the path produced by this algorithm.
Input Format
The first line contains two positive integers and separated by a space, denoting the number of nodes and the number of edges. The next lines each contain three nonnegative integers and (, ), representing an edge with weight . The testdata guarantees that the graph is connected.
Output Format
Output a single real number, the expected XOR sum of the path produced by the algorithm above, with three decimal places. (It is recommended to use data types with higher precision.)
2 2
1 1 2
1 2 3
2.333
Hint
Sample Explanation
With probability you go directly from node to node , and the XOR sum of this path is ; with probability you traverse the self-loop at node once and then go to node , and the XOR sum is ; with probability you traverse the self-loop at node twice and then go to node , and the XOR sum is ; and so on. Therefore, the expected value of the XOR sum is $\dfrac{3}{2}+\dfrac{1}{4}+\dfrac{3}{8}+\dfrac{1}{16}+\dfrac{3}{32}+\cdots=\dfrac{7}{3}$, which is approximately .
Constraints
- of the testdata satisfies .
- of the testdata satisfies , , and the graph may contain multiple edges or self-loops.
Translated by ChatGPT 5
京公网安备 11011102002149号