我是靠谱客的博主 专注帽子,最近开发中收集的这篇文章主要介绍Robot Rapping Results Report (拓扑排序唯一 + 二分),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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 ().

The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi), indicating that robot ui beat robot vi 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.

Examples

Input

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

Output

4

Input

3 2
1 2
3 2

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.

题意:n个选手经过m场比赛,输入数据a b 代表a打赢了b,问最少在第几场比赛后才能确定唯一的名词(拓扑排序唯一)。如果没有的话,输出-1.

思路:由于要确定唯一的拓扑排序,数据量还比较大。因此我们可以才用二分法来减少查找次数。在检查的时候,我们采用队列的形式,因为如果是唯一的排序,那么每次都可以找出一个入度为0的点,我们采用队列维护,这样就可以判断拓扑排序是否唯一的了。

代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define N 100010
using namespace std;
vector<int>E[N];
int n,u[N],v[N],f[N];
int check(int mid)
{
for(int i=1; i<=n; i++) //清空有向图
E[i].clear();
memset(f,0,sizeof(f));//初始化入度
for(int i=1; i<=mid; i++) //创建有向图,并计算入度
{
E[u[i]].push_back(v[i]);
f[v[i]]++;
}
queue<int>Q;
for(int i=1; i<=n; i++) //把入度为0的入队列,代表它是最前面的数
if(!f[i])
Q.push(i);
while(!Q.empty())
{
if(Q.size()!=1)//如果存在多个的话,说明拓扑排序不唯一
return 0;
int t=Q.front(); //取队首
Q.pop();
for(int i=0; i<E[t].size(); i++) //把被这个点打败的人的入度-1
{
int m=E[t][i];
f[m]--;
if(!f[m]) //入度为0,入队列
Q.push(m);
}
}
return 1;//维护队列的时候已经确定了拓扑排序唯一
}
int main()
{
int m;
scanf("%d%d",&n,&m);
for(int i=1; i<=m; i++) //输入对战双方
scanf("%d%d",&u[i],&v[i]);
int flag=-1; //没有唯一解时为-1
int l=1,r=m;
while(l<=r) //二分法,减少查找次数
{
int mid=(l+r)/2;//中间值
if(check(mid)) //如果在mid前找到的话,减小区间,继续查找
{
r=mid-1;
flag=mid;
}
else
l=mid+1;
}
printf("%dn",flag);
}

 

最后

以上就是专注帽子为你收集整理的Robot Rapping Results Report (拓扑排序唯一 + 二分)的全部内容,希望文章能够帮你解决Robot Rapping Results Report (拓扑排序唯一 + 二分)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部