The Valley of Mexico
Time limit1sMemory limit128 MB
Given a graph on cities placed in convex position, find a crossing-free Hamiltonian path (visiting every vertex once) that is lexicographically smallest, or report none.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Greedy, Geometry
- Solved
- No attempts yet
Problem
Mexico City sits in a beautiful valley known as the Valley of Mexico, which long ago was mostly a lake. Around the year 1300, Aztec religious leaders decreed that the center of the lake be filled in to build the capital of their empire. Today the lake is completely covered.
Before the Aztecs arrived, cities stood on the shores around the lake. Some of these cities had commercial agreements with one another. Goods were traded by boat between any two cities that shared an agreement, and any two cities could be joined by a straight line segment drawn across the lake.
The kings eventually organized this commerce into a single commerce route that connected every city around the lake. The route had to satisfy all of the following:
- It could start in any city, but had to end in a city different from the start.
- It visited every city exactly once.
- Every two consecutively visited cities shared a commercial agreement.
- Every two consecutively visited cities were joined by a line segment across the lake.
- To keep the boats from colliding, the route never crossed itself.

In the figure the lines (both thick and thin) are commercial agreements; the thick lines form a commerce route that starts at city 2 and ends at city 5, and this route never crosses itself. A route such as would be illegal, because it would cross itself.
The cities are numbered through in clockwise order around the lake.
Given and the list of commercial agreements, construct a commerce route that meets every requirement above.
Input
- Line 1: the integer .
- Line 2: an integer , the number of commercial agreements.
- Next lines: each contains two space-separated integers, the two cities joined by one commercial agreement. Each agreement is listed once and is undirected.
Output
Several different commerce routes may satisfy every requirement (in particular, a route and its reverse are both valid). Output the lexicographically smallest one.
Read a route as the sequence of city numbers in the order they are visited. Route is smaller than route if, at the first position where they differ, has the smaller city number. Print the chosen route on lines, the -th line holding the -th city visited.
If no commerce route satisfies every requirement, print a single line containing .
Constraints
- — the number of cities around the lake.