D. Robot Rapping Results Report

题目连接:

http://www.codeforces.com/contest/655/problem/D

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<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
vector<int>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<int>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<E[now].size();i++)
{
int v = E[now][i];
in[v]--;
if(in[v]==0)Q.push(v);
}
}
return true;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&E1[i],&E2[i]);
int l=1,r=m,ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))r=mid-1,ans=mid;
else l=mid+1;
}
printf("%d\n",ans);
}

CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 二分+拓扑排序的更多相关文章

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

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

  2. CROC 2016 - Elimination Round (Rated Unofficial Edition) F - Cowslip Collections 数论 + 容斥

    F - Cowslip Collections http://codeforces.com/blog/entry/43868 这个题解讲的很好... #include<bits/stdc++.h ...

  3. CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp

    E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...

  4. CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp

    E. Intellectual Inquiry 题目连接: http://www.codeforces.com/contest/655/problem/E Description After gett ...

  5. CROC 2016 - Elimination Round (Rated Unofficial Edition) C. Enduring Exodus 二分

    C. Enduring Exodus 题目连接: http://www.codeforces.com/contest/655/problem/C Description In an attempt t ...

  6. CROC 2016 - Elimination Round (Rated Unofficial Edition) B. Mischievous Mess Makers 贪心

    B. Mischievous Mess Makers 题目连接: http://www.codeforces.com/contest/655/problem/B Description It is a ...

  7. CROC 2016 - Elimination Round (Rated Unofficial Edition) A. Amity Assessment 水题

    A. Amity Assessment 题目连接: http://www.codeforces.com/contest/655/problem/A Description Bessie the cow ...

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

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

  9. Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)

    题目链接:https://codeforces.com/contest/1100/problem/E 题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最 ...

随机推荐

  1. 深入理解Spring系列之四:BeanDefinition装载前奏曲

    转载 https://mp.weixin.qq.com/s?__biz=MzI0NjUxNTY5Nw==&mid=2247483835&idx=1&sn=276911368d4 ...

  2. docker之构建redis-cluster集群

    下载和编译redis安装包 参考:https://www.cnblogs.com/cwp-bg/p/8094914.html # 从官方网站下载安装包,注意,当前在哪个目录下执行命令,下载的包将在哪个 ...

  3. Linux系统中提示/usr/bin/ld: cannot find -lxxx错误的通用解决方法

    在linux环境编译应用程式或lib的source code时常常会出现如下的错误讯息: 代码如下: /usr/bin/ld: cannot find -lxxx 这些讯息会随着编译不同类型的sour ...

  4. Deep Learning基础--Softmax求导过程

    一.softmax函数 softmax用于多分类过程中,它将多个神经元的输出,映射到(0,1)区间内,可以看成概率来理解,从而来进行多分类! 假设我们有一个数组,V,Vi表示V中的第i个元素,那么这个 ...

  5. Vim的分屏功能

    本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 f ...

  6. 主机名/etc/hosts文件的作用

    1,/etc/hosts,主机名ip配置文件. # Do not remove the following line, or various programs # that require netwo ...

  7. java经典面试题大全

    基本概念 操作系统中 heap 和 stack 的区别 什么是基于注解的切面实现 什么是 对象/关系 映射集成模块 什么是 Java 的反射机制 什么是 ACID BS与CS的联系与区别 Cookie ...

  8. docvalue and fielddata

    大部分字段类型默认被索引的(inverted index),可以被搜索search: 哪个文档有这个词sort&aggregations: look up the document and f ...

  9. 洛谷 P1652圆 题解

    题目传送门 这道题也就是考你对几何的了解: 圆与圆没有公共点且一个圆在另一个圆外面时,叫做圆与圆相离. 当圆心距大于两圆半径之和时,称为两圆外离: 当圆心距小于两圆半径之差的绝对值时,称为两圆内含. ...

  10. 洛谷P1319压缩技术 题解

    题目传送门 这道题是入门难度的题.特别水...QWQ...... #include<bits/stdc++.h> using namespace std; *],top; int main ...