362. Depth First Order
Starting from vertex 1, print the vertices in depth-first order: follow one route as far as it goes before backing up and trying another.
When several neighbours are available, visit the SMALLER vertex number first.
Print only the vertices reachable from vertex 1.
The code is problem 361 with the queue replaced by a stack, which is the entire difference between the two traversals. There is one wrinkle: a stack returns what was pushed last, so to visit the smallest neighbour first you must push the neighbours in DESCENDING order.
Constraints - `1 ≤ n ≤ 100000` - `0 ≤ m ≤ 200000` - There are no self-loops and no repeated edges.
Input
The first line contains two integers n and m, the number of vertices and
edges. Each of the next m lines contains two integers u and v, an
undirected edge between those vertices. Vertices are numbered 1 to n.
Output
Print the reachable vertices in depth-first order from vertex 1, separated by single spaces.