#K43647. Taco BFS Traversal

    ID: 27356 Type: Default 1000ms 256MiB

Taco BFS Traversal

Taco BFS Traversal

In this problem, you are given a directed graph representing web pages and the links between them. You need to perform a Breadth-First Search (BFS) traversal starting from a given node. The task is to output the order in which the nodes are visited in the BFS traversal. Note that the traversal only follows the direction of the edges.

The BFS algorithm is defined as follows:

  1. Start from the given node ( s ).
  2. Visit the node and enqueue all its directly connected neighbors (if they have not been visited yet).
  3. Dequeue a node and repeat the process until all reachable nodes have been visited.

Ensure that each node is visited at most once. This method is widely used in web crawling and graph processing tasks.

inputFormat

The first line contains two integers (n) and (m) representing the number of nodes and the number of directed edges respectively. Each of the next (m) lines contains two integers (u) and (v), representing an edge from node (u) to node (v). The last line contains an integer (s), the starting node for the BFS traversal.

outputFormat

Output a single line with the nodes in the order they are visited by the BFS, separated by a single space.## sample

5 4
1 2
1 3
3 4
4 5
1
1 2 3 4 5