John's Trip
Time limit1sMemory limit128 MB
Find an Euler circuit in a connected multigraph that uses every street exactly once and is lexicographically smallest by street sequence, starting at the smaller endpoint of the first street.
- Level
Medium7 of 10
- Topics
- Graph, DFS, Greedy, Implementation
- Solved
- No attempts yet
Problem
Little Johnny has got a new car and decided to drive around town to visit his friends. He wanted to visit every one of them, and there were many: one friend lived on each street. Thinking about how to make his trip as short as possible, he soon realized that the best plan was to drive along every street of the town exactly once. Naturally, he wanted to finish the trip at the place where he started — his parents' house.
The streets of Johnny's town are numbered with integers from to , where . The junctions are numbered separately with integers from to , where . All junctions have different numbers. Each street connects exactly two junctions, which need not be different. No two streets share the same number. If more than one such round trip exists, Johnny chooses the one whose sequence of street numbers is lexicographically smallest.
All streets are two-way, and from every street you can reach every other street in town. However, the streets are very narrow, so once the car enters a street it cannot turn back. Assume Johnny lives at the junction with the smaller number among the two junctions of the first street in the input.
Write a program that finds the desired round trip. If no such round trip exists, output the corresponding message.
Input
The input consists of several blocks, each describing one town. Each line of a block contains three integers , , , where and are the junctions connected by the street numbered . The end of a block is marked by a line with . The end of the input is an additional empty block, i.e. a line with .
Output
For each input block, print one line containing the street numbers of Johnny's round trip, in the order he drives them, separated by single spaces. If no round trip can be found, print the message Round trip does not exist. on that line instead. Separate the outputs of consecutive blocks with a single empty line.