#D6749. Breadth First Search
Breadth First Search
Breadth First Search
Write a program which reads an directed graph , and finds the shortest distance from vertex to each vertex (the number of edges in the shortest path). Vertices are identified by IDs .
Constraints
Input
In the first line, an integer denoting the number of vertices, is given. In the next lines, adjacent lists of vertex are given in the following format:
...
is ID of the vertex and denotes its degree. are IDs of vertices adjacent to .
Output
For each vertex , print and in a line. is ID of vertex and is the distance from vertex to vertex . If there are no path from vertex to vertex , print -1 as the shortest distance. Print in order of IDs.
Example
Input
4 1 2 2 4 2 1 4 3 0 4 1 3
Output
1 0 2 1 3 2 4 1
inputFormat
Input
In the first line, an integer denoting the number of vertices, is given. In the next lines, adjacent lists of vertex are given in the following format:
...
is ID of the vertex and denotes its degree. are IDs of vertices adjacent to .
outputFormat
Output
For each vertex , print and in a line. is ID of vertex and is the distance from vertex to vertex . If there are no path from vertex to vertex , print -1 as the shortest distance. Print in order of IDs.
Example
Input
4 1 2 2 4 2 1 4 3 0 4 1 3
Output
1 0 2 1 3 2 4 1
样例
4
1 2 2 4
2 1 4
3 0
4 1 3
1 0
2 1
3 2
4 1
</p>