#P2296. [NOIP 2014 提高组] 寻找道路

[NOIP 2014 提高组] 寻找道路

Description

In a directed graph GG where every edge has length 11, you are given a start and a terminal. Please find a path from the start to the terminal that satisfies the following conditions:

  1. For every vertex on the path, all endpoints of its outgoing edges are directly or indirectly able to reach the terminal.
  2. Among all paths satisfying condition 1, the path should be the shortest.

Note: Graph GG may contain multiple edges and self-loops. It is guaranteed that the terminal has no outgoing edges.

Please output the length of a path that satisfies the conditions.

Input Format

The first line contains two integers nn and mm separated by a space, denoting that the graph has nn vertices and mm edges.

Each of the next mm lines contains two integers x,yx, y separated by a space, indicating there is a directed edge from vertex xx to vertex yy.

The last line contains two integers s,ts, t separated by a space, indicating that the start is ss and the terminal is tt.

Output Format

Output a single line containing an integer, the length of the shortest path that satisfies the problem statement. If such a path does not exist, output 1-1.

3 2
1 2
2 1
1 3
-1
6 6
1 2
1 3
2 6
2 5  
4 5
3 4
1 5
3

Hint

Sample 1 Explanation.

As shown in the figure above, arrows represent directed roads and dots represent cities. The start 11 is not connected to the terminal 33, so a path satisfying the problem statement does not exist, hence the output is 1-1.

Sample 2 Explanation.

As shown in the figure above, a path that satisfies the conditions is 13451 \to 3 \to 4 \to 5. Note that vertex 22 cannot appear in the answer path because vertex 22 has an edge to vertex 66, and vertex 66 cannot reach the terminal 55.

Constraints.

  • For 30% of the testdata, 0<n100 < n \le 10, 0<m200 < m \le 20.
  • For 60% of the testdata, 0<n1000 < n \le 100, 0<m20000 < m \le 2000.
  • For 100% of the testdata, 0<n1040 < n \le 10^4, 0<m2×1050 < m \le 2 \times 10^5, 0<x,y,s,tn0 < x, y, s, t \le n, x,stx, s \ne t.

Translated by ChatGPT 5