XOR Racer II
Jamie is gaming instead of focusing on AUCPL Division A again, but he thinks it's fine, as long as he gets a good score. The game is called XOR Racer, and like the name suggests, you win by getting the largest possible XOR score. Every level in this game is defined by a tree, and there are many levels. Each node represents a room, and each edge represents a bidirectional corridor, and thus each level is a separate building.
Each corridor has a certain numerical value, and you can start in any room for each level. Then, you can proceed to walk through the corridors however you like, collecting the different corridor values and XORing them together. For each level, you want to determine the maximum possible value you can obtain. Then, you want to sum these values over the levels to report your total maximum score, so Jamie can win the game.
Thus, Jamie is tasked with calculating the maximum XOR sum of any path in each of the trees representing the levels, and summing them together.
Input
The first line contains two integers
and
, the number of rooms in the entire game and the number of total corridors between them. The rooms are labelled
to
.
The next lines each contain the details of a single undirected corridor. This takes the form of three integers
,
, and
, the rooms which the corridor connects and the numerical value assigned to it.
It is guaranteed that the rooms and corridors form a valid forest (that is, collection of trees).
Output
Output a single integer, the sum of the maximum XOR path sum between any two rooms between.
Clarifications
- Trees with a single node contribute
to the total score, as there are no paths Jamie can take.
- You might need to select pypy3 if you want to use Python to solve this.
Example
Input 1
3 2
1 2 3
2 3 1
Output 1
3
The possible paths are with path sum
,
with path sum
, and
with path sum
. So the maximum is
, and there is only one tree in this forest.
Input 2
6 4
1 2 5
2 3 6
4 5 1
5 6 2
Output 2
9
In this instance, there are two trees. For the first tree, the possibilities are . We take
. For the second tree the possibilities are
, so we take
. Thus the sum is
.
Comments