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 ;
}

CodeForces - 645D Robot Rapping Results Report(拓扑排序)的更多相关文章

  1. codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)

    题目链接: D. Robot Rapping Results Report time limit per test 2 seconds memory limit per test 256 megaby ...

  2. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分

    题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...

  3. CodeForces 645D Robot Rapping Results Report

    二分,拓扑排序. 二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$0$的节点,那么不可行. #pragma comment(linker, "/STACK:102400 ...

  4. Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】

    题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...

  5. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 二分+拓扑排序

    D. Robot Rapping Results Report 题目连接: http://www.codeforces.com/contest/655/problem/D Description Wh ...

  6. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

    题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...

  7. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  8. 【CF645D】 Robot Rapping Results Report(拓扑排序,二分)

    题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序, 若没有唯一拓扑序输出-1 思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<&g ...

  9. 【【henuacm2016级暑期训练】动态规划专题 O】Robot Rapping Results Report

    [链接] 我是链接,点我呀:) [题意] 让你确定一个最小的k 使得1..k这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...

随机推荐

  1. 4-1 线程安全性-原子性-atomic-1

    我们发现在不做任何同步的情况下,我们计算的累加结果是错误的. com.mmall.concurrency.example.count.CountExample2 C:\Users\ZHONGZHENH ...

  2. 【codevs2495】水叮当的舞步

    题目描述 Description 水叮当得到了一块五颜六色的格子形地毯作为生日礼物,更加特别的是,地毯上格子的颜色还能随着踩踏而改变.为了讨好她的偶像虹猫,水叮当决定在地毯上跳一支轻盈的舞来卖萌~~~ ...

  3. Chrome,firefox,ie等浏览器空格&nbsp;宽度不一样

    方案一:使用其他字符代替空格 使用(&nbsp:)空格浏览器之间,显示的不一样,对不齐等现象. 解决方案: 用半角空格&ensp:或者全角空格&emsp:就可以了,&e ...

  4. ArcEngine开发遇到的问题(转)

    ArcEngine开发遇到的问题 https://blog.csdn.net/u013751758/article/category/6971559 转载 2018年02月11日 17:28:11 1 ...

  5. Build Path

    ------------siwuxie095 什么是 Build Path? 为什么使用 Build Path? 如: (1)创建一个Java工程:LearnBuildPath (2)点击 Next, ...

  6. 190. Reverse Bits 二进制相反数

    [抄题]: Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 E ...

  7. hadoop搭建好,启动服务后,无法从web界面访问50070

    在hadoop完全分布式搭建好以后,从主节点启动正常,使用jps查看启动的进程,正常,在几个从节点上使用jps查看,显示正常,但从web上输入下面网址: http://主节点IP:50070 无法正常 ...

  8. Free GIS Software

    Refer to There are lots of free gis software listed in the website: http://www.freegis.org/ http://w ...

  9. Linux命令累积

    常用命令 ipconfig  -查看本机ip.接口等信息 ping ip   -ping远程服务器或终端 cd ~      -返回根目录 cd .. 返回上级目录 cd ../..  返回上两级目录 ...

  10. - Unknown tag (c:set).

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>