博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 二分+拓扑排序...
阅读量:4968 次
发布时间:2019-06-12

本文共 2368 字,大约阅读时间需要 7 分钟。

D. Robot Rapping Results Report

题目连接:

Description

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.

Sample Input

4 5

2 1
1 3
2 3
4 2
4 3

Sample Output

4

Hint

题意

有n个机器人,然后打了m场比赛

m场比赛描述是:A打败了B

如果A打败B,B打败C,那么A就能打败C,具有传递性

现在问你,最少只需要前多少场比赛就能够知道顺序了。

或者说无论如何都不能知道,输出-1

题解:

二分,然后check的时候,我们检查他的拓扑序是否是一条直线就好了

代码

#include
using namespace std;const int maxn = 1e5+7;vector
E[maxn];int n,m,E1[maxn],E2[maxn];int in[maxn];bool check(int mid){ memset(in,0,sizeof(in)); for(int i=0;i<=n;i++)E[i].clear(); for(int i=1;i<=mid;i++) E[E1[i]].push_back(E2[i]),in[E2[i]]++; queue
Q; for(int i=1;i<=n;i++) if(in[i]==0) Q.push(i); while(!Q.empty()) { int now = Q.front(); Q.pop(); if(Q.size())return false; for(int i=0;i

转载于:https://www.cnblogs.com/qscqesze/p/5301540.html

你可能感兴趣的文章
[spfa] Jzoj P4722 跳楼机
查看>>
代码审计入门后审计技巧
查看>>
Linux-Rsync服务器/客户端搭建实战
查看>>
接口和抽象类有什么区别
查看>>
简单通过百度api自动获取定位-前端实现
查看>>
180117 我的宠物识别判断语句
查看>>
JavaScript修炼之道pdf
查看>>
自己动手构造编译系统++编译、汇编与链接pdf
查看>>
JAVA 中文件读写函数BufferedReader 和 BufferedWriter 的使用
查看>>
Codeforces Round #206 (Div. 2)
查看>>
提升混合应用页面打开速度的新思路
查看>>
Mycat分表分库
查看>>
2019.7.11
查看>>
Php取扩展名
查看>>
模板的文件名和方法名一定要一致!!
查看>>
**p
查看>>
优先队列详解
查看>>
VS2012 创建项目失败,,提示为找到约束。。。。
查看>>
通过给定的文件流,判断文件的编码类型
查看>>
zookeeper(3) 持久化
查看>>