Module: bfs. advanced course


Problem

1/3

0-1 BFS: Beginning (C++)

Theory Click to read/hide

0-1 BFS
To solve this problem, we modify the standard BFS algorithm using deques ( deque ): if the considered edge has a weight of 0, then we will add a vertex to the beginning, otherwise to the end. 
Thus, at the beginning of the deque there will always be a vertex, the distance to which is less than or equal to the distance to the other vertices of the deque, and the requirement to arrange elements in the deque in non-decreasing order is preserved.
For the implementation of the 0-1 BFS algorithm, see the problem itself.

Problem

Given an image of an undirected graph (has edges of weight 0 and 1), print a list of the shortest distances from vertex 0 to all others.
 
Input 
An image of an undirected graph with edges 0 and 1 is given.
 
Output
In your answer, output a list of shortest paths from vertex 0.