题目链接:http://codeforces.com/contest/655/problem/D

大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系。

解法是二分答案,利用拓扑排序看是否所有关系被唯一确定。即任意一次只能有1个元素入度为0入队。

 #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set> using namespace std; const int N=; struct Edge {
int to,next;
Edge() {}
Edge(int _to,int _nxt):to(_to),next(_nxt) {}
} edge[N<<];
int idx=,head[N];
void addedge(int u,int v) {
edge[++idx]=Edge(v,head[u]);
head[u]=idx;
}
int in[N],que[N]; int a[N],b[N]; bool topoSort(int n,int mid){
idx=;memset(head,,sizeof head);
memset(in,,sizeof in);
for (int i=;i<=mid;i++) {
addedge(a[i],b[i]);
in[b[i]]++;
}
int tail=;
for (int i=;i<=n;i++)
if (in[i]==)
que[tail++]=i;
if (tail>) return false;
for (int i=;i<tail;i++){
int cnt=;
for (int k=head[que[i]];k;k=edge[k].next){
int v=edge[k].to;
in[v]--;
if (in[v]==){
cnt++;
if (cnt>) return false;
que[tail++]=v;
}
}
}
return true;
} int main() {
int n,m;
scanf("%d %d",&n,&m);
for (int i=;i<=m;i++) {
scanf("%d %d",a+i,b+i);
}
int low=,high=m,ret=-;
while (low<=high) {
int mid=(low+high)>>;
if (topoSort(n,mid)) {
ret=mid;
high=mid-;
}
else
low=mid+;
}
printf("%d\n",ret);
return ;
}

CF #CROC 2016 - Elimination Round 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. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

    题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...

  3. CodeForces - 645D Robot Rapping Results Report(拓扑排序)

    While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some a ...

  4. 【CF645D】 Robot Rapping Results Report(拓扑排序,二分)

    题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序, 若没有唯一拓扑序输出-1 思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<&g ...

  5. Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】

    题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...

  6. codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)

    题目链接: D. Robot Rapping Results Report time limit per test 2 seconds memory limit per test 256 megaby ...

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

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

  8. 8VC Venture Cup 2016 - Elimination Round A. Robot Sequence 暴力

    A. Robot Sequence 题目连接: http://www.codeforces.com/contest/626/problem/A Description Calvin the robot ...

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

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

随机推荐

  1. Nginx+IIS+Redis 处理Session共享问题 1

    最近遇到一个棘手的问题,微信公众平台的前端站点session老是丢失,我们是走的微信网页授权,授权后获取用户openid,丢失后没有openid后续的操作全白搭了,因为没了openid只能判断为客户不 ...

  2. WeMall的Android app商城中的wemall doraemon代码

    WeMall-Android 包含SMSSDK/WeMall-Client/social_sdk_library_project三个项目以及Api目录下的client.php/update.xml接口 ...

  3. 关于nodeJS多线程的支持,目前看来无法实现,讲解v8的一些东西

    关于这个,我这几天一直在研究,国内关于v8的资料很少,stackoverflow上也不多. 说起来我得说声抱歉,虽然并没有承诺什么.这个功能大概是无法实现.下面我来解释一下为什么. 首先我们要了解一下 ...

  4. 1751: [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 150[Su ...

  5. c# 将匿名类或者集合转Json格式数据一些方法

    要说写这个功能呢也是因为工作需要,白天呢上班写个Web页面需要ajax请求后台并将数据以Json格式传会前端,由于公司特殊性吧,不能连外网(很苦比).所以只有等到晚上回家上网边查边写! public ...

  6. (6)简单说说java中的线程

    先甩出来两种创建线程的方法: private static int count = 100; public static void main(String[] args) { // 用继承Thread ...

  7. 【转】Objective-C Runtime

    之前在找Runtime资料,这篇条理是相对比较清晰,对我最有启发的一篇,转载以作记录. 对于iOS小白,值得多看几遍,会有不少收获. --------------------------------- ...

  8. 【树莓派】iptables相关配置

    关于iptables的配置,参见官方资料:http://wiki.ubuntu.org.cn/IptablesHowTo 最好. 进入iptables # sudo iptables -L 列出目前的 ...

  9. Tcl与Design Compiler (三)——DC综合的流程

    本文属于原创手打(有参考文献),如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 1.基本流程概述 首先 ...

  10. mysql创建数据表时如何判断是否已经存在?

    >>> create table if not exists people(name text,age int(2),gender char(1)); 如上代码表示创建一个名为peo ...