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

Copy
4 5
2 1
1 3
2 3
4 2
4 3
output

Copy
4
input

Copy
3 2
1 2
3 2
output

Copy
-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,代表n个点m条边,下面m行代表u为v的父亲节点,求当恰好给出多少条边时可以得到所有点的次序。

解题思路:在建树时同时记录以下这条边时给出的第几条边,用边权记录。用ans记录答案。进行拓扑排序,拓扑排序时一旦同时出现两个及以上的0入度点,则说明有多个点的次序无法准确排序,那么结果就是-1,否则向队列压入0入度点的同时记录一下ans = max(ans, 边权),整个拓扑排序结束后,这个ans就是所求结果。因为这样就相当于记录了最高次序的那个点所连接的下一个0入度点之间的边权值,也就是最多需要给出的边。

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
const int MaxN = 1e5;
const int Inf = << ;
typedef struct
{
int to, od;
} Power; vector <Power> num[MaxN+];
int n, m, ans;
int ind[MaxN+]; bool Toposort()
{
queue <int> que;
for(int i = ;i <= n;i++)
{
if(ind[i] == ) que.push(i);
}
int u;
Power v;
while(!que.empty())
{
u = que.front();
que.pop();
if(!que.empty()) return ;
for(int i = ;i < num[u].size();i++)
{
v = num[u][i];
ind[v.to]--;
if(!ind[v.to])
{
ans = max(ans, v.od);
que.push(v.to);
}
}
}
return ;
} int main()
{
cin >> n >> m;
int a, b;
Power S;
for(int i = ;i <= m;i++)
{
scanf("%d %d", &a, &b);
S.to = b;S.od = i;
num[a].push_back(S);
ind[b]++;
}
if(!Toposort()) printf("-1\n");
else printf("%d\n", ans);
return ;
}

最新文章

  1. Window10+VS2015+DevExpress.net 15.1.7完美破解(图)
  2. oracle学习笔记系列------oracle 基本操作之基本函数的用法
  3. Machine Learning for hackers读书笔记(七)优化:密码破译
  4. JVM --java 字节码的结构解析
  5. 用户界面线程AfxBeginThread的使用
  6. Android JNI programming demo with Eclipse
  7. RunJS推荐用于个人使用(使用方便JS、css实时预览、编辑、管理等功能)
  8. 使用github出了些问题?fatal: unable to access;Failed connect to github.com:8087;
  9. 一步一步带你实现virtual dom(一)
  10. R语言︱词典型情感分析文本操作技巧汇总(打标签、词典与数据匹配等)
  11. AD服务无法启动
  12. sjms-2 创建型模式
  13. FromData获取表单数据
  14. 慢工出细活 JS 等待加载效果
  15. sqlserver2008r2安装
  16. Dell笔记本Ubuntu无线网卡驱动安装
  17. excel自定义函数添加和使用方法
  18. Centos6.8下搭建SVN服务器
  19. uipath接入Python
  20. python模块之XlsxWriter

热门文章

  1. 408. Valid Word Abbreviation有效的单词缩写
  2. SparkR 读取数据&amp; Spark运行的配置
  3. Yii2验证登录得User类
  4. WCF把书读薄(4)——事务编程与可靠会话
  5. LightOJ 1065 Island of Survival (概率DP?)
  6. wp7启动+幻灯片效果
  7. 1235: 入学考试[DP]
  8. kali linux之搜索引擎Shodan
  9. kali linux之被动信息收集(dns信息收集,区域传输,字典爆破)
  10. 使用IIS服务器部署网页,需要开启服务里的“ASP.NET 状态服务”