题目链接:

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. project 2010 使用技巧

    快捷键 设置任务子任务 ALT+SHIFT+向右方向键 1.工作时间设置 新建一个日历后,可以在 “项目 >> 项目信息 >> 日历” 中进行选择

  2. jQuery基础知识--选择器与效果

    $(this).hide()-----隐藏当前元素 $("p").hide()------隐藏所有段落 $(".test").hide()--隐藏所有class ...

  3. Python之定向爬虫Scrapy

    1.Scrapy介绍 Scrapy,Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试 ...

  4. 网页绘制图表 Google Charts with JavaScript #1....好强、好简单啊!

    此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript....好强.好简单啊!#1 http://www.dotblogs.com.tw/mis2 ...

  5. Ubuntu kylin系统改中文系统文件名为英文

    刚装好系统,将使用语言改成了中文,结果重启后,提示是否将文件系统的名字改为新的,我一不注意,点了是...这样,在以后使用终端的时候,会有中文来干扰,所以需要改回英文. 方法如下: 输入两个命令即可: ...

  6. 用O(1)的时间复杂度,找到栈和队列中的最小(大)值

    最近刷剑指offer,看到两道编程题,考察在O(1)的复杂度内,找出最值. 觉得很有意思,很有借鉴意义,故记录在此. 需要注意的是,这里所说的O(1) 有个前提, 就是已经通过某种容器的存储方式进行初 ...

  7. virtualenv python虚拟环境搭建

    python virtualenv.py flask

  8. Web Design:给实验室UI们的一堂课(上)

    实验室的UI越来越水,设计什么的做的一塌糊涂,所以拖了很久,就想给他们讲一下设计或者说入门吧,上周末才倒出来时间. 这里放上PPT和讲稿吧,懒得去整理板式了. 主要讲了一下Web Design怎么做, ...

  9. 理解ruby on rails中的ActiveRecord::Relation

    ActiveRecord::Relation是rails3中添加的.rails2中的finders, named_scope, with_scope 等用法,在rails3统一为一种Relation用 ...

  10. Supporting Connected Routes to Subnet Zero

    Supporting Connected Routes to Subnet Zero IOS allows the network engineer to tell a router to eithe ...