To be, xor not to be

Time limit: 0.2s Memory limit: 512MB Input: Output:

The noble Stegan, a prince of Dendromark, was left with sorrow by passing of his beloved father. After tiresome and useless struggles to find out what fate his father met by computing the area of various trapezoid figures using Simpson's rule, when all hope was lost, a spirit came to his aid. And the spirit gave him a tree to harness and take into his care. And the spirit later told him: Look! Look closer! Do you see? This tree is special as it has an even number of vertices. Should you be able to find the best manner in which to pair these vertices so that the total cost of this pairing is as small as possible, you shall find how the passing of you father came to be. But first you shall figure out what is the cost of a pairing; its secret lies in the meaning of life.

Stegan, a great mathematician that he happens to be, knows that the true meaning of life can be found in the words: "To be xor not to be" (and in the lines that followed it, but that is of no concern to us). Therefore he deduced that, for a pair of vertices, the cost of their pairing is the bitwise xor of the weights of the edges that lie on the simple path that connects the two vertices.

Unfortunately, providence has not gifted Stegan with computer science skills and so he has not a clue how to come by the solution to his problem and he has come to you to beg for help.

Formally, you are given tree with NN vertices, where NN is even. Every edge has a weight. For a pair of vertices (u,v)(u,v) we define the cost to pair them up to be the bitwise XORXOR of the weights of the edges that lie on the simple path that connects the two vertices.

You have to find a way to split the vertices into N2\frac{N}{2} pairs such that the total sum of their costs is as small as possible. Because it might be too hard to ask you to find the actual minimum solution, we only ask you to do your best and you will receive points accordingly.

Input

The first line of the input contains an even integer, NN — the number of vertices in the tree. Each of the next N1N−1 lines will consist of 33 space-separated numbers ui,vi,wiu_i, v_i, w_i meaning that there is an edge of weight wiw_i that connects uiu_i and viv_i. Both uiu_i and viv_i are between 11 and NN.

Output

On the first line, you should print the total cost of the pairs of vertices you have chosen. On the next N2\frac{N}{2} lines, on each line you should print the indexes of nodes that should be paired together. All pairs must not have any common nodes.

Scoring

For each group:

  1. If there is a test case for which your program doesn't generate a valid response (Time Limit Exceeded, Runtime Error) then you will receive 00 points for that group.
  2. A response is not considered valid as well if the N2\frac{N}{2} pairs that you output are not a split of the NN nodes or do not correspond with the total cost that you output.
  3. If none of the above happen, then you will receive points according to the formula:

group_scoremin{(ok_costiout_costi)4}\displaystyle \text{group\_score} \cdot \text{min}\left\{ \left( \frac{ok\_cost_i}{out\_cost_i} \right) ^4\right\}, where:

  • ii goes through all the test cases in the test group
  • out_costiout\_cost_i is the cost that your solution computes for test case ii
  • ok_costiok\_cost_i is the optimal answer for test case ii

Constraints and notes

  • 2N2002 \leq N \leq 200
  • NN is even
  • 0wi2240 \leq w_i \leq 2^{24}
  • The bitwise XORXOR (^) operator returns a number whose binary representation has a 11 in each bit position for which the corresponding bits of either but not both operands are 11.
# Puncte Restricții
1 3 N10,wi64N \leq 10, w_i \leq 64
2 6 N14N \leq 14
3 19 N40,wi64N \leq 40, w_i \leq 64
4 8 N120,wi16N \leq 120, w_i \leq 16
5 41 N120N \leq 120
6 23 No further restrictions.

Example 1

stdin

6
5 2 42
5 4 52
6 3 26
4 6 39
1 6 15

stdout

54
1 6
3 5
4 2

Explanation

Example 2

stdin

4
1 2 4
3 4 5
2 3 1

stdout

1
2 3
1 4

Explanation

Log in or sign up to be able to send submissions!