atcoder#ABC287H. [ABC287Ex] Directed Graph and Query
[ABC287Ex] Directed Graph and Query
Score : points
Problem Statement
There is a directed graph with vertices and edges. The vertices are numbered through , and the -th directed edge goes from vertex to vertex .
The cost of a path on this graph is defined as:
- the maximum index of a vertex on the path (including the initial and final vertices).
Solve the following problem for each .
- Find the minimum cost of a path from vertex to vertex . If there is no such path, print
-1
instead.
The use of fast input and output methods is recommended because of potentially large input and output.
Constraints
- If , then .
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer for .
4 4
1 2
2 3
3 1
4 3
3
1 2
2 1
1 4
2
3
-1
For , the path via the -st edge from vertex to vertex has a cost of , which is the minimum.
For , the path via the -nd edge from vertex to vertex and then via the -rd edge from vertex to vertex has a cost of , which is the minimum.
For , there is no path from vertex to vertex , so -1
should be printed.