CF346D.Robot Control
省选/NOI-
通过率:0%
时间限制:6.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The boss of the Company of Robot is a cruel man. His motto is "Move forward Or Die!". And that is exactly what his company's product do. Look at the behavior of the company's robot when it is walking in the directed graph. This behavior has been called "Three Laws of Robotics":
- Law 1. The Robot will destroy itself when it visits a vertex of the graph which it has already visited.
- Law 2. The Robot will destroy itself when it has no way to go (that is when it reaches a vertex whose out-degree is zero).
- Law 3. The Robot will move randomly when it has multiple ways to move (that is when it reach a vertex whose out-degree is more than one). Of course, the robot can move only along the directed edges of the graph.
Can you imagine a robot behaving like that? That's why they are sold at a very low price, just for those who are short of money, including mzry1992, of course. mzry1992 has such a robot, and she wants to move it from vertex s to vertex t in a directed graph safely without self-destruction. Luckily, she can send her robot special orders at each vertex. A special order shows the robot which way to move, if it has multiple ways to move (to prevent random moving of the robot according to Law 3). When the robot reaches vertex t, mzry1992 takes it off the graph immediately. So you can see that, as long as there exists a path from s to t, she can always find a way to reach the goal (whatever the vertex t has the outdegree of zero or not).
Sample 2
However, sending orders is expensive, so your task is to find the minimum number of orders mzry1992 needs to send in the worst case. Please note that mzry1992 can give orders to the robot while it is walking on the graph. Look at the first sample to clarify that part of the problem.
该公司——机器人公司(Company of Robot)的老板是个残酷的人。他的座右铭是:“前进,否则毁灭!”而该公司的产品,也的确严格遵循这一信条。请观察该公司机器人在有向图中行走时的行为。这种行为被称作“机器人三定律”:
- 第一定律:当机器人访问到一个它此前已访问过的顶点时,它将自我毁灭;
- 第二定律:当机器人无路可走时(即到达一个出度为 0 的顶点),它将自我毁灭;
- 第三定律:当机器人有多个可选移动方向时(即到达一个出度大于 1 的顶点),它将随机选择一条出边移动。当然,机器人只能沿图中的有向边移动。
你能想象有机器人如此行事吗?正因如此,它们售价极低,专供囊中羞涩之人——包括 mzry1992 在内。mzry1992 就拥有一台这样的机器人,她希望安全地将它从有向图中的顶点 s 移动至顶点 t,且途中不发生自我毁灭。幸运的是,她可以在每个顶点向机器人发送特殊指令。所谓特殊指令,即在机器人面临多个移动选择时(为避免其依据第三定律随机移动),明确指示它应选择哪一条出边。当机器人抵达顶点 t 后,mzry1992 会立即将其从图中移除。因此,只要图中存在一条从 s 到 t 的路径,她总能设法达成目标(无论顶点 t 的出度是否为 0)。
样例 2
然而,发送指令代价高昂,因此你的任务是:求出在最坏情况下,mzry1992 所需发送的最少指令条数。请注意,mzry1992 可以在机器人行进过程中动态地在各个顶点发出指令。请参考第一个样例,以进一步明确该部分题意。
输入格式
The first line contains two integers n (1 ≤ n ≤ 106) — the number of vertices of the graph, and m (1 ≤ m ≤ 106) — the number of edges. Then m lines follow, each with two integers u__i and v__i (1 ≤ u__i, v__i ≤ n; v__i ≠ u__i), these integers denote that there is a directed edge from vertex u__i to vertex v__i. The last line contains two integers s and t (1 ≤ s, t ≤ n).
It is guaranteed that there are no multiple edges and self-loops.
第一行包含两个整数 n(1≤n≤106)—— 图的顶点数,以及 m(1≤m≤106)—— 边的数量。接下来有 m 行,每行包含两个整数 ui 和 vi(1≤ui,vi≤n;vi=ui),表示存在一条从顶点 ui 指向顶点 vi 的有向边。最后一行包含两个整数 s 和 t(1≤s,t≤n)。
保证图中不存在重边和自环。
输出格式
If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -1.
如果存在到达目标的方法,则输出最坏情况下所需的最少指令数;否则输出 -1。
输入输出样例
输入#1
4 6 1 2 2 1 1 3 3 1 2 4 3 4 1 4
输出#1
1
输入#2
4 5 1 2 2 1 1 3 2 4 3 4 1 4
输出#2
1
说明/提示
Consider the first test sample. Initially the robot is on vertex 1. So, on the first step the robot can go to vertex 2 or 3. No matter what vertex the robot chooses, mzry1992 must give an order to the robot. This order is to go to vertex 4. If mzry1992 doesn't give an order to the robot at vertex 2 or 3, the robot can choose the "bad" outgoing edge (return to vertex 1) according Law 3. So, the answer is one.
考虑第一个测试样例。初始时,机器人位于顶点 1。因此,在第一步中,机器人可以移动到顶点 2 或顶点 3。无论机器人选择哪一个顶点,mzry1992 都必须向机器人下达指令,该指令是前往顶点 4。如果 mzry1992 在顶点 2 或顶点 3 处未向机器人下达指令,则根据规则 3,机器人可以选择“坏”的出边(即返回顶点 1)。因此,答案为 1。
输入解题思路,AI测评打分。不知道怎么写?