#t509. 骑马修栅栏
骑马修栅栏
Riding Fences
Problem Background
Special for beginners, ^_^
Problem Description
Farmer John has many fences to repair every year. He always rides his horse through every fence and fixes the broken parts.
John is as lazy as any other farmer. He hates riding, so he never passes the same fence twice. You must write a program that reads the description of the fence network and computes a path to repair the fences such that every fence is traversed exactly once. John may start riding from any vertex (the intersection of two fences) and finish at any vertex.
Each fence connects two vertices, labeled from to (although some farms may not have vertices). Any number () of fences may meet at a single vertex. All fences are connected (that is, you can reach any fence from any other fence).
Your program must output the riding path (represented by the vertex numbers visited in order along the path). If we view the output path as a number in base , then when multiple solutions exist, output the one that is smallest in this base- representation (i.e. output the one with the smaller first number; if there is still a tie, the one with the smaller second number, and so on). The input guarantees at least one solution.
Input Format
The first line: an integer (), the number of fences.
Lines to : each line contains two integers () indicating that this fence connects vertex and vertex .
Output Format
The output should have lines, each containing one integer, representing the vertex numbers along the path in order. Note that there may be multiple solutions, but only the one required by the statement above is considered correct.
Sample Input
9
1 2
2 3
3 4
4 2
4 5
2 5
5 6
5 7
4 6
Sample Output
1
2
3
4
2
5
4
6
5
7
Hint
The graph is not necessarily simple; there may be multiple edges between two vertices.
Constraints
Time limit: 1s; memory limit: 1024 KiB for each test case.