## CCO '06 P5 - Domino

View as PDF

Points: 30
Time limit: 2.0s
Memory limit: 256M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### Canadian Computing Competition: 2006 Stage 2, Day 2, Problem 2

Crazy Cuckoo Charlie is a Computer Science student trying to get into the Book of Waterloo Records. Unfortunately, he has no special skills. He only has a lot of time, and a lot of dominoes. He's going to try to break the record for "Waterloo's Longest Domino Chain", which involves putting as many dominoes end-to-end as possible.

Dominoes have two halves. Each half has a number of dots on it. To break the Waterloo Record, Charlie must place all the dominoes end-to-end in a line. To make it more difficult, however, the Book of Waterloo Records editors have added an extra rule: the adjacent halves of each pair of neighbouring dominoes must share the same number of dots, as in the picture above.

Charlie wants to use all his dominoes when making the chain. But due to this rule, Charlie won't be able to make a chain using all his dominoes (one trivial example is 1 2 and 4 5). He's willing to go out and buy extra dominoes to complete his chain. Obviously he wants to buy as few dominoes as possible, to save money for more important things like pizza and pop.

Can you help Charlie figure out the minimum number of dominoes required to complete his chain?

#### Hint

Use the properties of Euler paths.

#### Input Specification

Input consists of a series of test cases. Each test case describes one possible domino collection. The first line contains an integer indicating the total number of test cases. Each test case begins a line with the integer , , the number of dominoes Charlie has. Each of the next lines describes a domino in his collection, in the form A B, , where and are positive integers indicating the number of dots on each half of the domino. Don't forget you can flip dominoes around!

#### Output Specification

For each test case, output on a line an integer , the minimum number of dominoes Charlie needs to buy in order to be able to make a chain with all his dominoes.

On the next lines should be a list of the dominoes Charlie needs, in the form A B. Print the list of dominoes in ascending lexicographical order (from smallest to largest). If there are multiple sets of dominoes (all of the minimum size) that Charlie could buy, pick the lexicographically smallest one.

To determine which domino is lexicographically least we use the following check: For two given dominoes A B where , we pick the domino with the lower . If both dominoes have the same we pick the domino with the lower .

#### Sample Input

2
4
1 10
2 8
8 10
4 606
6
3 8
3 5
1 3
1 5
1 8
5 8

#### Sample Output

1
1 4
1
1 3