#K92402. Domino Arrangement
Domino Arrangement
Domino Arrangement
You are given a set of n domino pieces. Each domino piece is represented by two integers. Your task is to determine if it is possible to arrange all dominoes in a line so that the adjacent pieces share a common number. In other words, if you have domino pieces represented as \( (a_1, b_1), (a_2, b_2), \dots, (a_n, b_n) \), you must arrange them into a sequence in which for every consecutive pair \((a_i, b_i)\) and \((a_j, b_j)\), one of the numbers from the first domino equals one of the numbers from the second domino.
If such an arrangement exists, output the sequence of dominoes (each domino by its two numbers in order, one domino per line). Otherwise, output No
.
Note: The solution is based on finding an Eulerian path in an undirected multigraph where vertices represent numbers and domino pieces represent edges. A necessary and sufficient condition for the existence of an Eulerian path is that at most two vertices have an odd degree.
inputFormat
The input is read from stdin with the following format:
- The first line contains a positive integer \( n \) denoting the number of domino pieces.
- Each of the next \( n \) lines contains two space-separated integers, representing the two numbers on a domino piece.
It is guaranteed that \( n \ge 1 \).
outputFormat
If an arrangement is possible, print exactly \( n \) lines. Each line should contain two space-separated integers representing a domino piece in the order of the chain such that adjacent dominoes share a common number.
If no arrangement exists, output a single line containing the string No
.
All output should be written to stdout.
## sample3
1 2
2 3
3 4
1 2
2 3
3 4
</p>