100 atcoder#ABC213D. [ABC213D] Takahashi Tour
[ABC213D] Takahashi Tour
Score : points
Problem Statement
The Republic of AtCoder has cities numbered through and roads numbered through . Road connects City and City bidirectionally. It is guaranteed that one can travel between every pair of cities using roads.
Takahashi will depart City and have a journey by repeating the following.
- If there are unvisited cities that are directly connected to the city Takahashi is in now, he goes to the city with the smallest number among those cities.
- Otherwise,- if he is in City , he ends the journey;
- otherwise, he goes to the city he was in just before visiting the current city for the first time.
Find the sequence of cities visited by Takahashi in the order he visits them.
Constraints
- It is possible to travel between every pair of cities using roads.
Input
Input is given from Standard Input in the following format:
Output
Print the sequence of cities visited by Takahashi in the order he visits them, including City at the beginning and end of the journey, with spaces in between.
4
1 2
4 2
3 1
1 2 4 2 1 3 1
His journey will be as follows.
- Start in City .
- The unvisited cities directly connected to City are City and . Go to the city with the smaller number, that is, City .
- The unvisited city directly connected to City is City . Go there.
- There is no unvisited city directly connected to City . Go back to City .
- There is no unvisited city directly connected to City . Go to City , where he was in just before visiting City for the first time.
- The unvisited city directly connected to City is City . Go there.
- There is no unvisited city directly connected to City . Go back to City .
- There is no unvisited city directly connected to City . End the journey.
5
1 2
1 3
1 4
1 5
1 2 1 3 1 4 1 5 1