Icy Itinerary
Time limit3sMemory limit1024 MB
Find an order of all n houses starting at house 1 where road moves and non-road moves switch at most once.
Problem
It is a harsh, cold winter in a town of houses, and everyone is asleep in the early morning. Snow lies deep on the ground except on the roads that connect some pairs of houses. Only Thomas is awake.
Thomas the mailman has to deliver mail to each of the houses. The houses are numbered from 1 to . He starts at his own house (house 1) and then visits all the other houses in some order. He can ride a bicycle between two houses connected by a road, and he can use skis between two houses with no road between them. Skis cannot be used on roads, and the bicycle cannot be used off roads. Switching between bicycle and skis is tedious, so he wants to switch at most once.
Find an ordering of the houses with such that there is at most one index () where either
- and are connected by a road, but and are not, or
- and are not connected by a road, but and are.
In other words, the sequence starts at 1 and switches between road and non-road moves at most once.
Input
The first line contains two integers and (, ), the number of houses and the number of roads.
Each of the next lines contains two integers and (), meaning that and are connected by a road. Roads can be used in both directions, and all unordered pairs are distinct.
A valid ordering exists for every input.
Output
Print integers on one line, the order in which to visit the houses. The first number must be 1.