A - Robot Rapping Results Report CodeForces - 645D

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.

题目意思是给你几对机器人然后比赛,问有没有唯一的一个序列确定他们的等级关系;

解题思路:就是用拓朴排序,然后加上邻接表,然后找到一个最大边即可;

代码如下:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct note
{
    int x,y,next;
}e[120000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int frist[101010],book[101010],b[101010];
        memset(frist,-1,sizeof(frist));
        memset(book,0,sizeof(book));
        int sum=0,flag=1;
        int i,j;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&e[i].x,&e[i].y);
            e[i].next=frist[e[i].x];
            frist[e[i].x]=i;   //初始化邻接表
            book[e[i].y]++;
        }
        for(i=1;i<=n;i++)
        {
            if(book[i]==0)
            {
              b[sum++]=i;     //找到入度为0的那个点
            }
        }
        if(sum!=1) flag=0;   //如果不是有且只有一个的话输出-1
        int num=0,maxx=-100;
        for(i=0;i<sum;i++)
        {   num=0;
            for(j=frist[b[i]];j!=-1;j=e[j].next)  //邻接表遍历每条边
            {
                book[e[j].y]--;
                if(book[e[j].y]==0)
                {
                    b[sum++]=e[j].y;     //有入度为0 的放入邻接表中
                    num++;
                    if(j>maxx)            //找到最大的边
                        maxx=j;
                }
            }
            if(num>1)                  //如果入度为0的不唯一就输出-1
               flag=0;
        } 
        if(flag==0||sum<n)             //如果不能遍历每条边输出-1;
            printf("-1\n");
        else
            printf("%d\n",maxx);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/TANG3223/article/details/82084281