Breadth-First Search


Problem Statement

Implement breadth-first search in an undirected graph.

During the traversal, always select the smallest unvisited vertex as the next one.

Input

Your first line will contain vv and ee as space-separated integers. vv represents the number of vertices in the graph, labelled from 00 to v−1v-1, and ee represents the number of edges in the graph.

Your next line will contain ss, the source vertex to start the DFS from.

Your next ee lines will contain 22 space-separated numbers, specifying that there is an edge between the two specified vertices.

Output

The BFS traversal, separated by line breaks.

Constraints

For all test cases:

  • 2≤v≤1032 \le v \le 10^3
  • 0≤s≤v−10 \le s \le v-1
  • 1≤e≤1061 \le e \le 10^6

Example 1

Input 1
6 5
0
0 1
0 2
1 3
1 4
2 5
Output 1
0
1
2
3
4
5

Python Template

Code 1
v, e = map(int, input().split())
s = input()
edges = [tuple(map(int, input().split(" "))) for _ in range(e)]

# print(...)

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.