题库练习 Robot Rapping Results Report
← 上一题 下一题 →

A10312 | Robot Rapping Results Report

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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$ (![](/uploads/acgo/image/3b65b834baa14974_e95a4d33f125.jpeg)).

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
C++ 编辑器
输入
输出