CF346D.Robot Control

普及/提高-

通过率:0%

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 ss to vertex tt 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 tt , mzry1992 takes it off the graph immediately. So you can see that, as long as there exists a path from ss to tt , she can always find a way to reach the goal (whatever the vertex tt 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.

输入格式

The first line contains two integers nn ( 1<=n<=1061<=n<=10^{6} ) — the number of vertices of the graph, and mm ( 1<=m<=1061<=m<=10^{6} ) — the number of edges. Then mm lines follow, each with two integers uiu_{i} and viv_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n ; viuiv_{i}≠u_{i} ), these integers denote that there is a directed edge from vertex uiu_{i} to vertex viv_{i} . The last line contains two integers ss and tt ( 1<=s,t<=n1<=s,t<=n ).

It is guaranteed that there are no multiple edges and self-loops.

输出格式

If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -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.

首页