CF645D.Robot Rapping Results Report

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

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 ii will beat robot jj if and only if robot ii has a higher skill level than robot jj . And if robot ii beats robot jj and robot jj beats robot kk , then robot ii will beat robot kk . 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 nn ( 2<=n<=1000002<=n<=100000 ) and the number of rap battles mm ().

The next mm lines describe the results of the rap battles in the order they took place. Each consists of two integers uiu_{i} and viv_{i} ( 1<=ui,vi<=n1<=u_{i},v_{i}<=n , uiviu_{i}≠v_{i} ), indicating that robot uiu_{i} beat robot viv_{i} in the ii -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 mm relations.

输出格式

Print the minimum kk such that the ordering of the robots by skill level is uniquely defined by the first kk rap battles. If there exists more than one ordering that satisfies all mm 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)(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)(1,3,2) and (3,1,2)(3,1,2) are possible orderings of the robots from strongest to weakest after both rap battles.

首页