CROC 2016 - Elimination Round
D
Robot Rapping Results Report
Codeforces
1800
原题
← CE →
翻译中…

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.

翻译中…

Input

翻译中…

The first line of the input consists of two integers, the number of robots $*n*$ ($2 ≤ *n* ≤ 100 000$) and the number of rap battles $*m*$ (image).

翻译中…

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.

翻译中…

Output

翻译中…

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.

翻译中…

Note

翻译中…

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 #1
4 5
2 1
1 3
2 3
4 2
4 3
Output #1
4
InputCopy OutputCopy
Input #2
3 2
1 2
3 2
Output #2
-1
InputCopy OutputCopy
题目信息
题号
645D
时间限制
2 s
内存限制
256 MB
标签
二分
动态规划
图论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。