Monk and the Islands
Practice
4 (184 votes)
Breadth first search
Easy
Graphs
Open
Problem
86% Success 35671 Attempts 20 Points 3s Time Limit 256MB Memory 1024 KB Max Code

Monk visits the land of Islands. There are a total of N islands numbered from 1 to N. Some pairs of islands are connected to each other by Bidirectional bridges running over water.
Monk hates to cross these bridges as they require a lot of efforts. He is standing at Island #1 and wants to reach the Island #N. Find the minimum the number of bridges that he shall have to cross, if he takes the optimal route.

Input:
First line contains T. T testcases follow.
First line of each test case contains two space-separated integers N, M.
Each of the next M lines contains two space-separated integers X and Y , denoting that there is a bridge between Island X and Island Y.

Output:
Print the answer to each test case in a new line.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 104
1 ≤ M ≤ 105
1 ≤ X, YN

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
7 votes
Tags:
Breadth-first searchGraphsAlgorithms
Points:20
29 votes
Tags:
AlgorithmsBreadth First SearchGraphsImplementationMatrix
Points:20
153 votes
Tags:
AlgorithmsApprovedBreadth First SearchEasyGraphsGreedy AlgorithmsMathOpen