QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 128 MB 總分: 100 可 Hack ✓

#16099. Finding Roads

统计

In a directed graph $G$, every edge has a length of $1$. Given a starting point and an ending point, find a path from the start to the end that satisfies the following conditions:

  1. For every vertex on the path, all vertices reachable from its outgoing edges must be directly or indirectly connected to the ending point.
  2. Among paths satisfying condition 1, find the shortest one.

Note: The graph $G$ may contain multiple edges and self-loops. It is guaranteed that the ending point has no outgoing edges.

Output the length of the path that satisfies these conditions.

Input

The first line contains two space-separated integers $n$ and $m$, representing the number of vertices and edges in the graph.

The next $m$ lines each contain two space-separated integers $x$ and $y$, representing a directed edge from vertex $x$ to vertex $y$.

The last line contains two space-separated integers $s$ and $t$, representing the starting point $s$ and the ending point $t$.

Output

Output a single integer representing the length of the shortest path satisfying the problem description. If no such path exists, output $-1$.

Examples

Input 1

3 2
1 2
2 1
1 3

Output 1

-1

Note 1

As shown in the figure above, arrows represent directed roads and dots represent cities. The starting point $1$ is not connected to the ending point $3$, so no path satisfying the conditions exists; therefore, output $-1$.

Input 2

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

Output 2

3

Note 2

As shown in the figure above, the path satisfying the conditions is $1 \to 3 \to 4 \to 5$. Note that vertex $2$ cannot be part of the answer path because vertex $2$ has an outgoing edge to vertex $6$, and vertex $6$ is not connected to the ending point $5$.

Constraints

For 30% of the data, $0 < n \le 10$, $0 < m \le 20$;

For 60% of the data, $0 < n \le 100$, $0 < m \le 2000$;

For 100% of the data, $0 < n \le 10000$, $0 < m \le 200000$, $0 < x, y, s, t \le n$, $x, s \ne t$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.