Just shortest distance problem
Practice
4.1 (35 votes)
Medium
Shortest path algorithm
Problem
88% Success 4221 Attempts 50 Points 3s Time Limit 256MB Memory 1024 KB Max Code

You are given an empty graph of N vertices and M queries of two types:

  1. Given a number X answer what is the shortest distance from the vertex 1 to the vertex X.
  2. Given two integers X, Y add the new oriented edge from X to Y.

Input
The first line contains two integers N and M. The following M lines describe queries in the format mentioned above

Output
For each query of the first type output one integer on a separate line - answer for the corresponding query. Output -1 if it's impossible to get to the vertex in this query from the vertex 1.

Constraints

  • 1 <= N <= 1000
  • 1 <= M <= 500 000
  • 1 <= X, Y <= N

Subtasks

  • N <= 500, M <= 5 000 in 50 % of test data

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:50
8 votes
Tags:
GraphsAlgorithmsBreadth-first searchC++
Points:50
33 votes
Tags:
Breadth First SearchGraphsMedium
Points:50
11 votes
Tags:
GraphsAlgorithmsBreadth-first searchTree