#C1740. Travel Sequence Through Cities
Travel Sequence Through Cities
Travel Sequence Through Cities
You are given a graph representing cities and bidirectional roads. The graph has n cities numbered from 1 to \( n \) and m roads. Starting at city 1, you are required to output the travel sequence obtained by performing a depth-first search (DFS). In this DFS, when visiting a city, you should consider its neighboring cities in increasing order. Formally, if the neighbors of a city \( u \) are \( v_1, v_2, \dots, v_k \) (with \( v_1 < v_2 < \dots < v_k \)), push them onto the stack in reverse order (i.e., \( v_k \) first), so that the smallest number is processed first.
Input Constraints:
- \( 1 \leq n \leq 10^5 \)
- \( 0 \leq m \leq 10^5 \)
- The input roads may not connect all cities, and some cities might be isolated.
Note: The DFS stops when no more cities can be visited from the starting city. The travel sequence should therefore only include the cities reachable from city 1.
inputFormat
The first line contains two integers \( n \) and \( m \), representing the number of cities and roads respectively. The following \( m \) lines each contain two integers \( u \) and \( v \), indicating that there is a bidirectional road between cities \( u \) and \( v \).
outputFormat
Output a single line containing the travel sequence starting from city 1. The cities should be printed in the order they are visited during the DFS, separated by a single space.
## sample5 4
1 2
1 3
2 4
3 5
1 2 4 3 5
</p>