QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100
[0]

# 10146. To be, xor not to be

Statistics

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 N vertices, where N is even. Every edge has a weight. For a pair of vertices (u,v) we define the cost to pair them up to be the bitwise XOR 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 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, N — the number of vertices in the tree. Each of the next N1 lines will consist of 3 space-separated numbers ui,vi,wi meaning that there is an edge of weight wi that connects ui and vi. Both ui and vi are between 1 and N.

Output

On the first line, you should print the total cost of the pairs of vertices you have chosen. On the next N2 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 0 points for that group.
  2. A response is not considered valid as well if the N2 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}, where:

  • i goes through all the test cases in the test group
  • out_costi is the cost that your solution computes for test case i
  • ok_costi is the optimal answer for test case i

Constraints and notes

  • 2N200
  • N is even
  • 0wi224
  • The bitwise XOR (^) operator returns a number whose binary representation has a 1 in each bit position for which the corresponding bits of either but not both operands are 1.
# Puncte Restricții
1 3 N10,wi64
2 6 N14
3 19 N40,wi64
4 8 N120,wi16
5 41 N120
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

problem_10146_1.png

Example 2

stdin

4
1 2 4
3 4 5
2 3 1

stdout

1
2 3
1 4

Explanation

problem_10146_2.png

Nobilul Stegan, un prinț al Dendromarcei, a rămas trist după moartea tatălui său. După multe încercări zadarnice de a afla soarta tatălui calculând aria diferitor trapeze folosind regula lui Simpson, un spirit i-a venit în ajutor. Spiritul i-a dat lui Stegan un arbore pentru a avea grijă de el. Spiritul i-a zis: _Uită-te! Mai aproape! Nu vezi? Acest arbore e special, deoarece are un număr par de vârfuri. Dacă vei găsi un mod de a împărți vârfurile în perechi astfel ca costul lor să fie cât mai mic posibil, tu vei afla cum ți-a murit tatăl. Dar mai întâi trebuie să îți dai seama care e costul unei perechi. Secretul ține de sensul vieții._

Stegan, fiind un matematician, știe că sensul vieții poate fi găsit în cuvintele: "To be xor not to be" ( frază care nu poate fi tradusă în limba română). El a dedus că pentru o pereche de vârfuri, costul perechii este valoarea operației XOR (SAU exclusiv) pe biți pe greutățile muchiilor care se află pe drumul între aceste vârfuri.

În detrimentul lui Stegan, el e lipsit de abilități computaționale, de aceea te roagă pe tine să-l ajuți.

Formal, îți este dat un arbore cu N vârfuri, N fiind par. Fiecare muchie are o greutate. Pentru o pereche de vârfuri (u,v) noi definim costul perechii ca valoarea operației XOR (SAU exclusiv) pe biți pe greutățile muchiilor care se află pe drumul între aceste vârfuri.

Tu trebuie să găsești un mod de a împărți vârfurile în N2 perechi astfel ca suma costurilor să fie cât mai mică posibilă. Deoarece poate fi dificil să aflați suma minimă a costurilor, noi vă rugăm doar să faceți cât de bine puteți, fiind punctați corespunzător.

Date de intrare

Prima linie de intrare conține un număr întreg par, N - numărul de vârfuri în arbore. Următoarele N1 linii conțin 3 numere separate prin spațiu, ui,vi,wi, însemnând că vârfurile ui și vi sunt conectate printr-o muchie cu greutatea wi.

Date de ieșire

Pe prima linie afișați suma costurilor perechilor de vârfuri pe care le-ai ales. Pe următoarele N2 linii afișați indicii vârfurilor care formează fiecare pereche. Nicio pereche nu trebuie să conțină vârfuri în comun.

Punctare

Pentru fiecare grup:

  1. Dacă este un test pentru care depășești limita de timp sau primești Runtime Error, vei primi 0 puncte pentru acel grup.
  2. Dacă este un test în care oricare din cele N2 perechi afișate nu este validă sau suma costurilor nu corespunde cu valoarea afișată, vei primi 0 puncte pentru acel grup.
  3. Dacă nici una din cele de sus nu se aplică, vei primi puncte după următoarea formulă:

group\_scoremin{(ok_costiout_costi)4}, unde:

  • i trece prin fiecare test din grup
  • out_costi este răspunsul afișat pentru testul i
  • ok_costi este răspunsul optim pentru testul i

Restricții și precizări

  • 2N200
  • N este par
  • 0wi224
# Puncte Restricții
1 3 N10,wi64
2 6 N14
3 19 N40,wi64
4 8 N120,wi16
5 41 N120
6 23 Fără restricții suplimentare.

Exemplul 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

Explicație

problem_10146_1.png

Exemplul 2

stdin

4
1 2 4
3 4 5
2 3 1

stdout

1
2 3
1 4

Explicație

problem_10146_2.png