cho.sh
Notes
Loading...

DFS and BFS

Time limit

2s

Memory limit

128 MB

Problem

Write a program that prints the order in which the given graph is visited by depth-first search (DFS), and then by breadth-first search (BFS). When several vertices can be visited next, always visit the vertex with the smallest number first. Stop when there are no more vertices reachable from the current traversal. The vertices are numbered from 1 through N.

Input

The first line contains three integers N, M, and V: the number of vertices, the number of edges, and the starting vertex of the traversal.

  • 1 <= N <= 1,000
  • 1 <= M <= 10,000

Each of the next M lines contains two vertex numbers connected by an edge. There may be multiple edges between the same pair of vertices. All edges are undirected.

Output

Print the DFS visit order on the first line and the BFS visit order on the second line. In each line, print the vertices in the order they are visited, starting from V.