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 ≤ n, ui ≠ 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.

还真让我说对了,这就是拓扑排序!题意是这样的:给你一个有向图,然后让你遍历,判断拓扑排序是否唯一,如果唯一的话就输出你是在第几个边添加之后判断出来的,否则输出-1.

思路:你跑一遍拓扑排序,看看能行不,同时记录一下编号最大边,最后输出的时候用。

#include<iostream>
#include<string.h>
using namespace std;
#define N 100010
#define M 200010
struct node
{
    int x,y,z;
}a[N];
int n,m,k,b[N],c[N],d[N];
int main()
{
    while(cin>>n>>m)
    {
        memset(b,-1,sizeof(b));
        memset(d,0,sizeof(d));
        k=0;
        for(int i=0;i<m;i++)
        {
            int x,y;
            cin>>x>>y;
            a[k].x=x;
            a[k].y=y;
            a[k].z=b[x];
            b[x]=k++;
            d[y]++;
        }
        int ans=0,flag=1,st=0;
        for(int i=1;i<=n;i++)
            if(d[i]==0)
            c[st++]=i;
        if(st>1)
            flag=0;
        for(int i=0;i<st;i++)
        {
            int sum=c[i],t=0;
            for(int j=b[sum];~j;j=a[j].z)
            {
                if(--d[a[j].y]==0)
                {
                    c[st++]=a[j].y;
                    t++;
                    if(j>ans)
                        ans=j;
                }

            }
            if(t>1)
                flag=0;
        }
        if(!flag||st<n)
            cout<<-1<<endl;
        else
            cout<<ans+1<<endl;
    }
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值