279. Shortest Path (Unweighted)
EasyBFS
Print the length of the shortest path (number of edges) from node 1 to node n in an undirected graph, or -1 if unreachable.
Input format
Line 1: n m Next m lines: u v
Output format
The distance, or -1.
Example 1
Input
5 5 1 2 2 3 3 5 1 4 4 5
Output
2
Example 2
Input
3 1 1 2
Output
-1
Constraints
2 ≤ n ≤ 10⁵ 0 ≤ m ≤ 2 × 10⁵
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…