QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100 Hackable ✓

#16099. 寻找道路

統計

在有向图 $G$ 中,每条边的长度均为 $1$,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:

  1. 路径上的所有点的出边所指向的点都直接或间接与终点连通。
  2. 在满足条件 1 的情况下使路径最短。

注意:图 $G$ 中可能存在重边和自环,题目保证终点没有出边。

请你输出符合条件的路径的长度。

输入格式

第一行有两个用一个空格隔开的整数 $n$ 和 $m$,表示图有 $n$ 个点和 $m$ 条边。

接下来的 $m$ 行每行 $2$ 个整数 $x,y$,之间用一个空格隔开,表示有一条边从点 $x$ 指向点$y$。

最后一行有两个用一个空格隔开的整数 $s, t$,表示起点为 $s$,终点为 $t$。

输出格式

输出只有一行,包含一个整数,表示满足题目描述的最短路径的长度。如果这样的路径不存在,输出$-1$。

样例数据

样例 1 输入

3 2
1 2
2 1
1 3

样例 1 输出

-1

样例 1 解释

problem_16099_1.png

如上图所示, 箭头表示有向道路,圆点表示城市。 起点 $1$ 与终点 $3$ 不连通,所以满足题目描述的路径不存在,故输出$-1$。

样例 2 输入

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

样例 2 输出

3

样例 2 解释

problem_16099_2.png
如上图所示,满足条件的路径为 $1 \to 3 \to 4 \to 5$。注意点 $2$ 不能在答案路径中,因为点 $2$ 连了一条边到点 $6$ ,而点 $6$ 不与终点 $5$ 连通。

限制与约定

对于30%的数据,$0 < n \le 10$,$0 < m \le 20$;

对于60%的数据,$0 < n \le 100$,$0 < m \le 2000$;

对于100%的数据,$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.