303331: CF645D. Robot Rapping Results Report

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Robot Rapping Results Report

题意翻译

#### 题目简述 $n$ 个机器人,每个机器人有一个不同的级别,级别介于 $1\sim n$,高级别的可以打败低级别的,现在给出 $n$ 个机器人的 $m$ 场比赛胜负情况,问最少需要前几场比赛就可以确定每个机器人的级别。 #### 输入格式 第一行两个整数 $n$ 和 $m$ 表示机器人个数和比赛场数,之后 $m$ 行每行两个整数 $u$ 和 $v$ 表示机器人 $u$ 打败机器人 $v$。 #### 输出格式 如果这 $m$ 场比赛可以确定每个机器人的级别则输出最少需要前几场比赛就可以确定,否则输出 $-1$。 #### 数据范围 $2\leq n\leq 10^5$,$1\leq m\leq \min(\frac{n\times (n-1)}{2},10^5)$。

题目描述

While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot $ i $ will beat robot $ j $ if and only if robot $ i $ has a higher skill level than robot $ j $ . And if robot $ i $ beats robot $ j $ and robot $ j $ beats robot $ k $ , then robot $ i $ will beat robot $ k $ . Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level.

输入输出格式

输入格式


The first line of the input consists of two integers, the number of robots $ n $ ( $ 2<=n<=100000 $ ) and the number of rap battles $ m $ (![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF645D/ff5e51057dd7573544b70b3883f7e12532c00133.png)). The next $ m $ lines describe the results of the rap battles in the order they took place. Each consists of two integers $ u_{i} $ and $ v_{i} $ ( $ 1<=u_{i},v_{i}<=n $ , $ u_{i}≠v_{i} $ ), indicating that robot $ u_{i} $ beat robot $ v_{i} $ in the $ i $ -th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all $ m $ relations.

输出格式


Print the minimum $ k $ such that the ordering of the robots by skill level is uniquely defined by the first $ k $ rap battles. If there exists more than one ordering that satisfies all $ m $ relations, output -1.

输入输出样例

输入样例 #1

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

输出样例 #1

4

输入样例 #2

3 2
1 2
3 2

输出样例 #2

-1

说明

In the first sample, the robots from strongest to weakest must be $ (4,2,1,3) $ , which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both $ (1,3,2) $ and $ (3,1,2) $ are possible orderings of the robots from strongest to weakest after both rap battles.

Input

题意翻译

#### 题目简述 $n$ 个机器人,每个机器人有一个不同的级别,级别介于 $1\sim n$,高级别的可以打败低级别的,现在给出 $n$ 个机器人的 $m$ 场比赛胜负情况,问最少需要前几场比赛就可以确定每个机器人的级别。 #### 输入格式 第一行两个整数 $n$ 和 $m$ 表示机器人个数和比赛场数,之后 $m$ 行每行两个整数 $u$ 和 $v$ 表示机器人 $u$ 打败机器人 $v$。 #### 输出格式 如果这 $m$ 场比赛可以确定每个机器人的级别则输出最少需要前几场比赛就可以确定,否则输出 $-1$。 #### 数据范围 $2\leq n\leq 10^5$,$1\leq m\leq \min(\frac{n\times (n-1)}{2},10^5)$。

加入题单

算法标签: