King's Inspection
Time limit10sMemory limit512 MB
Find the lexicographically smallest directed tour that starts and ends at city 1 and visits every other city exactly once.
- Level
Hard8 of 10
- Topics
- Graph, Backtracking, DFS
- Solved
- No attempts yet
Problem
King Karl is a responsible and diligent ruler. Every year he travels across his country to check that all cities are doing well.
There are cities and roads in his country. To control the travelers, every road is one way: a road from city to city cannot be used to go from to .

Karl wants to travel along the roads so that he starts in the capital, visits every non capital city exactly once, and finishes in the capital again.
As the transport minister, you have to find such a route, or determine that no such route exists.
Input
The first line contains two integers and (, ), the number of cities and the number of roads.
Each of the next lines contains two integers and (), meaning that there is a one way road from city to city . Cities are numbered from 1 to , and the capital is city 1. The same road may be given several times, and a road with may be given as well.
Output
If a route exists that starts in the capital, passes through every non capital city exactly once and finishes in the capital, print the city numbers of the route on one line, separated by single spaces. Print the capital both at the beginning and at the end of the route. If several such routes exist, compare the numbers from the front and print the lexicographically smallest one.
If there is no such route, print There is no route, Karl! on one line.