题目链接: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. html细节积累-01

    语义错误 块级元素可以包含内联元素和某些块级元素,内联元素不能包含块级元素,只能包含内联元素 页面可能正常解析,但不符合语义.浏览器自带容错机制,对于不规范的写法也能够正确解析,各浏览器的容错机制不同 ...

  2. 1596: [Usaco2008 Jan]电话网络

    1596: [Usaco2008 Jan]电话网络 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 601  Solved: 265[Submit][S ...

  3. 四大组件之一---------activity的知识

    activity的生命活动 activity的四种启动模式 Android中以一个任务栈用来管理activity 一个栈的形式进行管理 在清单文件中,通过<activity>标签的andr ...

  4. (4)Object对象的几个常用方法

    Object对象是java中对象的始祖,其有一些方法是经常需要我们来改写的: toString方法 该方法是Object的方法,Object的这的方法返回的是对象的文件结构加上对象的hashcode, ...

  5. 用Qemu搭建aarch32学习环境

    作者信息 作者: 彭东林 邮箱: pengdonglin137@163.com QQ: 405728433 软件平台 主机: Ubuntu14.04 64位版本 模拟器:Qemu-2.8.0 Linu ...

  6. ArrayList去除重复元素(包括字符串和自定义对象)

    1.去除重复字符串 package com.online.msym; import java.util.ArrayList; import java.util.Iterator; @SuppressW ...

  7. 模块化规范Common.js,AMD,CMD

    随着网站规模的不断扩大,嵌入网页中的javascript代码越来越大,开发过程中存在大量问题,如:协同开发,代码复用,大量文件引入,命名冲突,文件依赖. 模块化编程称为迫切的需求. 所谓的模块,就是实 ...

  8. 串的模式匹配和KMP算法

    在对字符串的操作中,我们经常要用到子串的查找功能,我们称子串为模式串,模式串在主串中的查找过程我们成为模式匹配,KMP算法就是一个高效的模式匹配算法.KMP算法是蛮力算法的一种改进,下面我们先来介绍蛮 ...

  9. sui的一些方法封装

    + function($) { "use strict"; var today = new Date(); var getDays = function(max) { var da ...

  10. JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换

    本篇博客我们就来聊一下Spring框架中的观察者模式的应用,即事件的发送与监听机制.之前我们已经剖析过观察者模式的具体实现,以及使用Swift3.0自定义过通知机制.所以本篇博客对于事件发送与监听的底 ...