题目链接:

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

题意:

题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k。

题解:

二分k的取值,做拓扑排序的时候只要每次只有一个元素没有前驱就可以唯一了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
using namespace std; const int maxn = ;
int n, m; struct Edge {
int v, ne;
Edge(int v, int ne) :v(v), ne(ne) {}
Edge() {}
}egs[maxn*]; int head[maxn], tot; void addEdge(int u, int v) {
egs[tot] = Edge(v, head[u]);
head[u] = tot++;
} int ind[maxn];
bool ok(int m) {
memset(ind, , sizeof(ind));
for (int i = ; i <= m; i++) {
ind[egs[i].v]++;
}
queue<int> Q;
for (int i = ; i < n; i++) {
if (ind[i] == ) {
Q.push(i);
}
}
while (!Q.empty()) {
if (Q.size() > ) return false;
int u = Q.front(); Q.pop();
int p = head[u];
while (p != -) {
Edge& e = egs[p];
if (p <= m) {
ind[e.v]--;
if (ind[e.v] == ) {
Q.push(e.v);
}
}
p = e.ne;
}
}
return true;
} void init() {
memset(head, -, sizeof(head));
tot = ;
} int main() {
while (scanf("%d%d", &n, &m) == && n) {
init();
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v); u--, v--;
addEdge(u, v);
}
int l = -, r = tot-;
while (l + < r) {
int mid = l + (r - l) / ;
if (!ok(mid)) l = mid;
else r = mid;
}
//printf("l:%d\n", l);
if (!ok(r)) printf("-1\n");
else printf("%d\n", r + );
}
return ;
}

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 二分+拓扑排序

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

  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. 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

随机推荐

  1. linux下不重启添加硬盘

    linux下热加载磁盘 临时给虚拟机加了一块硬盘,增加后懒得重启,于是看了看热加载 [root@centos5 ~]# cat /proc/scsi/scsiAttached devices:Host ...

  2. Windows2008下搭建NFS实现windows空间提供linux使用

    我们既然是要把Windows 的硬盘给Linux使用  就需要在Windows上安装NFS网络文件系统 操作如下 1.添加角色-----文件服务---勾选网络文件服务 2.安装完成后 我们需要把这30 ...

  3. 【转】HTML5新增元素兼容旧浏览器方法

    ref:http://www.jb51.net/html5/163906.html 问题:如何让IE8-兼容这些标签?(需要设计JS中的DOM) 代码如下: <span style=" ...

  4. jquery.pagination +JSON 动态无刷新分页

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlPage.aspx.cs& ...

  5. js获取和设置DOM样式函数cssStyle(类似于jquery的$(elem).css())

    如题,相信这个函数百度一搜一大推,但令人匪夷所思的是这些函数都写的“奇形怪状的”,例如http://www.cnblogs.com/windows7/archive/2010/03/30/170064 ...

  6. VPN连接失败

    连接VPN是总提示 本来我以为是VPN服务器的问题,可是别人就能连接成功,所以只能是我自己机子的问题.后来检查了一下连接属性,终于发现了问题: 这里“允许使用这些协议”应该是处于选中状态,而我的属性里 ...

  7. php中的日期

    1.在PHP中获取日期和时间 time()返回当前时间的 Unix 时间戳. getDate()返回日期/时间信息. gettimeofday()返回当前时间信息.date_sunrise()返回给定 ...

  8. delphi 基础之三 编写和调用dll文件

    delphi 编写和调用dll文件   Windows 的执行文件可以划分为两种形式程序和动态连接库 (DLLs).一般程序运行是用.EXE文件,但应用程序有时也可以调用存储在DLL的函数. 在如下几 ...

  9. Delphi XE5 for android 图片缩放和拖动处理

    首先,需要分辨手势的类型. 有两种类型的手势: 一是标准手势(Standard Gestures): 在Windows,android上,标准手势都是用一个手指. 在Mac OS X and iOS上 ...

  10. 如何在MySQL中获得更好的全文搜索结果

    如何在MySQL中获得更好的全文搜索结果 很多互联网应用程序都提供了全文搜索功能,用户可以使用一个词或者词语片断作为查询项目来定位匹配的记录.在后台,这些程序使用在一个SELECT 查询中的LIKE语 ...