问题:

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.

思路:

首先拓扑排序每次只能找到一个能力最大了,还要n个都要找出来,然后就是找用到了第几组。

代码:

#define N 120000
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,f[N],book[N],b[N];
struct node
{int a,b,net;
}e[N];
int main()
{while(~scanf("%d%d",&n,&m)){memset(f,-1,sizeof(f));memset(book,0,sizeof(book));memset(b,0,sizeof(b));int l=0,flag=1,num=0,ma=-0x3f3f3f3f;for(int i=1; i<=m; i++){scanf("%d%d",&e[i].a,&e[i].b);e[i].net=f[e[i].a];//用邻接表去储存,把a打败b看作有a点到b点有条有向边f[e[i].a]=i;book[e[i].b]++;}for(int i=1; i<=n; i++)if(book[i]==0)b[l++]=i;//找到能力大的if(l!=1)flag=0;//如果能力最大的不唯一,则拓扑排序不唯一for(int i=0; i<l; i++){num=0;for(int j=f[b[i]]; j!=-1; j=e[j].net){book[e[j].b]--;//b[i]已经被排好序了,比他小的入度减少;if(!book[e[j].b])//e[i].b是当前能力最大的{b[l++]=e[j].b;//入队列,已经入队列的点表示被排好序的num++;//当前能力最大的ma=j>ma?j:ma;//记录最多用到了第几组就能有唯一的拓扑排序}}if(num>1)flag=0;//拓扑排序不唯一}if(flag==0||l<n)printf("-1\n");elseprintf("%d\n",ma);}return 0;
}

Robot Rapping Results Report CodeForces - 645D相关推荐

  1. Educational Codeforces Round 100 (Rated for Div. 2)

    文章目录 Educational Codeforces Round 100 (Rated for Div. 2) A. Dungeon B. Find The Array C. Busy Robot ...

  2. COMP0037 Coursework Investigating Path Planning Algorithms

    COMP0037 Coursework 1 Term 2, 2019 "Path Planning in a Known World" Investigating Path Pla ...

  3. Robotframework集成jenkins执行用例

    Robotframework+jenkins配置 假设我们完成了一个模块的用例设计,可是想晚上9点或凌晨运行,这时候该怎么实现呢?jenkins可以很好解决我们的疑难. Jenkins安装 这里简单说 ...

  4. ICRA2022 SLAM相关论文整理

    目录 视觉SLAM相关 线.面.3Dfeature 多相机系统 VIO+GPS 其他 数据集 三维重建 语义SLAM Special sensor Lidar相关 视觉SLAM相关 线.面.3Dfea ...

  5. COMP0037 Coursework

    COMP0037 Coursework 1 Term 2, 2019 "Path Planning in a Known World" Investigating Path Pla ...

  6. 多测师肖sir_高级金牌讲师_jenkins持续集成测试(001)

    Jenkins操作手册 =================================================================== 一.jenkins介绍 1.持续集成(C ...

  7. [转] ICRA2022 SLAM相关论文整理

    目录 视觉SLAM相关 线.面.3Dfeature 多相机系统 VIO+GPS 其他 数据集 三维重建 语义SLAM Special sensor Lidar相关 视觉SLAM相关 线.面.3Dfea ...

  8. 基于ISE的设计实现基础

    所谓实现(Implement)是将综合输出的逻辑网表翻译成所选器件的底层模块与硬件原语,将设计映射到器件结构上,进行布局布线,达到在选定器件上实现设计的目的.实现主要分为3个步骤:翻译(Transla ...

  9. 查看tomcat启动文件都干点啥---server对象

    在上一章查看tomcat启动文件都干点啥---Catalina.java中说道了构造Server,,这次尝试着说一下Tomcat中Server的内容,首先看一下org.apache.catalina. ...

  10. 浙南联合训练赛20180414

    这次题目的代码都不长,CF的一贯风格 A - Game CodeForces - 513A Two players play a simple game. Each player is provide ...

最新文章

  1. pycharm导包错误
  2. CentOS6.3编译安装Nginx1.4.7 + MySQL5.5.25a + PHP5.3.28
  3. 【Android 安全】DEX 加密 ( Application 替换 | 替换 LoadedApk 中的 Application mApplication 成员 )
  4. 转载:linux+arm 网卡故障调试:ethtoolphy寄存器读写
  5. linux时间格式怎么写,linux基础--时间格式
  6. gitlab project项目迁移
  7. php异步处理,执行系统命令
  8. 可以在循环体内声明局部变量吗?
  9. 自动化测试平台搭建从零开始
  10. easyUI之ComboBox(下拉列表框)
  11. Error mounting /dev/sdc1 at /media/XXXX: Command-line `mount -t “ntfs“ -o
  12. 山东省计算机二级c语言题,2012计算机二级C语言题库.doc
  13. 计算机时钟电路检查,数字电子时钟电路设计实训报告
  14. 弯管机程序使用三菱FX系列 PLC和昆仑通态触摸屏,也可以用三菱F940系列触摸屏
  15. 毕业论文答辩PPT模板
  16. KNN算法和kd树详解(例子+图示)
  17. 【图形学】计算机图形学的应用领域
  18. 如何用csdn上传资源
  19. OpenCV (c++)使用KDTree时,得到正确结果后报Segmentation fault (core dumped)
  20. 帝国CMS仿玩游戏网源码大型游戏资讯网站源码

热门文章

  1. php 易宝支付,易宝支付
  2. 解决OneNote同步失败的问题
  3. 【引用】43种名车标志及来历
  4. JWT令牌生成与校验
  5. nuvoton ADC采集
  6. Java学习笔记Day2:流程控制
  7. angularjs+chosen的使用备忘
  8. stLFR(single tube Long Fragment Read)介绍
  9. Spark之火可以燎原——访Spark亚太研究院院长、首席专家王家林
  10. DNF盗号木马之突破令牌密保