Depth-first search on a graph · Compiler

Depth-first search on a graph

Explore a two-way network from s using recursive depth-first search. Record each vertex on first arrival. Visit the smallest unvisited neighbor, finish exploring from it, then resume at the current vertex. Stop when the search from s is complete.

Input
The first line contains n m s. Vertices are numbered 0 through n-1 (1 <= n <= 50, 0 <= m <= 200). Each of the next m lines contains u v for an edge joining u and v in both directions. Endpoints differ and each unordered pair occurs at most once. The start s is a vertex ID.
Output
Print the reachable vertex IDs once each, in discovery order on one space-separated line.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…