题目链接:

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. ADO .NET 链接 增删改查

    ADO.NET: 数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连 ...

  2. CentOS用户权限管理--su与sudo

    Linux权限管理--su与sudo 1.su用来切换登录的用户,比如当前用户为chen,可以用su zhu,并输入用户zhu的登录密码,就可以切换到用户zhu.如果一个普通用户想切换到root用户, ...

  3. encodeURIComponent编码后java后台的解码

    解决方法一: JavaScript: window.self.location="searchbytext.action?searchtext="+encodeURICompone ...

  4. C# Async与Await用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. HBase分布式安装

    安装HBase之前需要先安装Hadoop,因为HBase是运行在Hadoop集群上的.安装Hadoop可以参照http://www.cnblogs.com/stGeekpower/p/3307289. ...

  6. 学习jQuery在表单信息修改之中的常用方法;

    submitHandler: function (form) { var parm = $("#myform").serialize(); var url = $("#m ...

  7. JDBC基础二

    1.配置文件:dbinfo.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test user ...

  8. xtrabackup之Innobackupex全备恢复

    一.当前环境 [mysql@hadoop1 ~]$ mysql --defaults-/my.cnf -uroot -p123456 -P3306 mysql> show variables l ...

  9. PuTTY 中文教程

    PuTTY 中文教程 更新记录 2006-11-29初步完成想写的这些东西 2007-06-11PuTTY 的最新版本到了0.6:修改了一下 SSH 隧道:添加了 SSH 反向隧道:添加了用 SSH ...

  10. laravel 笔记

    1.excel composer require maatwebsite/excel ~2.0.0 Maatwebsite\Excel\ExcelServiceProvider::class, 'Ex ...