CF982F.The Meeting Place Cannot Be Changed

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping.

Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements.

彼得是布拉金斯市的一名侦探。某人从一家银行盗走了巨额资金,彼得的任务是将其抓获。有人告诉彼得,一辆豪华轿车正沿着道路不停行驶。

彼得知道驾驶这辆轿车的正是劫匪。布拉金斯市的道路均为单向道路,每条道路连接两个路口。彼得希望选出一个路口,使得:无论劫匪从何处出发,只要他们持续沿道路无限行驶下去,就迟早会到达该路口。劫匪的初始位置未知。请找出满足上述要求的一个路口。

输入格式

The first line of the input contains two integers nn and mm (2≤n≤1052 \leq n \le 10^5, 2≤m≤5⋅1052 \leq m \leq 5 \cdot 10^5) — the number of intersections and the number of directed roads in Braginsk, respectively.

Each of the next mm lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i) — the start and finish of the ii-th directed road. It is guaranteed that the robbers can move along the roads indefinitely.

输入的第一行包含两个整数 nn 和 mm(2≤n≤1052 \leq n \le 10^5,2≤m≤5⋅1052 \leq m \leq 5 \cdot 10^5),分别表示布拉金斯市的交叉路口数量和有向道路数量。

接下来的 mm 行中,每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n,ui≠viu_i \ne v_i),表示第 ii 条有向道路的起点与终点。保证劫匪可以沿着道路无限地移动。

输出格式

Print a single integer kk — the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print −1-1.

输出一个整数 kk —— Petr 需要选择的交叉路口。如果存在多个答案,输出任意一个即可。如果不存在这样的交叉路口,则输出 −1-1。

输入输出样例

  • 输入#1

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

    输出#1

    3
  • 输入#2

    3 3
    1 2
    2 3
    3 1

    输出#2

    1

说明/提示

In the first example the robbers can move, for example, along the following routes: (1−2−3−1)(1-2-3-1), (3−4−5−3)(3-4-5-3), (1−2−3−4−5−3−1)(1-2-3-4-5-3-1). We can show that if Petr chooses the 33-rd intersection, he will eventually meet the robbers independently of their route.

在第一个例子中,劫匪可以沿以下路径移动,例如:(1−2−3−1)(1-2-3-1)、(3−4−5−3)(3-4-5-3)、(1−2−3−4−5−3−1)(1-2-3-4-5-3-1)。我们可以证明:如果彼得选择第 33 个路口,那么无论劫匪选择哪条路径,他最终都会与劫匪相遇。

输入解题思路,AI测评打分。不知道怎么写?

首页