二分,拓扑排序。

二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$0$的节点,那么不可行。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
int n,m,u[maxn],v[maxn];
int h[maxn],sz,r[maxn];
struct Edge{int u,v,nx;}e[maxn]; void add(int a,int b)
{
e[sz].u=a; e[sz].v=b; e[sz].nx=h[a];
h[a]=sz++;
} bool check(int x)
{
memset(h,-,sizeof h); sz=;
memset(r,,sizeof r);
for(int i=;i<=x;i++) add(u[i],v[i]),r[v[i]]++;
queue<int>Q; int num=; for(int i=;i<=n;i++) if(r[i]==) Q.push(i),num++; if(num!=) return ; while(!Q.empty())
{
int top=Q.front(); Q.pop();
num=; for(int i=h[top];i!=-;i=e[i].nx)
{
r[e[i].v]--;
if(r[e[i].v]==)
{
num++;
Q.push(e[i].v);
}
} if(num>) return ;
}
return ;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++) scanf("%d%d",&u[i],&v[i]);
check(); int L=,R=m,ans=-;
while(L<=R)
{
int m=(L+R)/;
if(check(m)) ans=m,R=m-;
else L=m+;
}
printf("%d\n",ans);
return ;
}

CodeForces 645D Robot Rapping Results Report的更多相关文章

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

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

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

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

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

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

  4. 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 ...

  5. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

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

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

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

  7. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

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

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

  9. 【【henuacm2016级暑期训练】动态规划专题 O】Robot Rapping Results Report

    [链接] 我是链接,点我呀:) [题意] 让你确定一个最小的k 使得1..k这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...

随机推荐

  1. c#计算2个字符串的相似度

    直接来代码 public static float levenshtein(string str1, string str2) { //计算两个字符串的长度. int len1 = str1.Leng ...

  2. MongoDB的.Net驱动

    mongo的驱动主要使用了两个,即samus和官方驱动. 个人感觉差别不大,且官方驱动也支持了LinQ.但在使用DBRef的时候,发现samus的驱动似乎不太好用,并没有达到想要的效果,也许是我的使用 ...

  3. [转]UIWebView的Javascript运行时对象

    An alternative, that may get you rejected from the app store, is to use WebScriptObject. These APIs ...

  4. 基于Hadoop开发网络云盘系统客户端界面设计初稿

    基于Hadoop开发网络云盘系统客户端界面设计初稿 前言: 本文是<基于Hadoop开发网络云盘系统架构设计方案>的第二篇,针对界面原型原本考虑有两个方案:1.类windows模式,文件夹 ...

  5. NHibernate Session-per-request and MiniProfiler.NHibernate

    NHibernate Session-per-request and MiniProfiler.NHibernate 前言 1.本文以mvc3为例,借鉴开源项目 NerdDnner项目完成nhiber ...

  6. Archives for the category: Fisheye/Crucible

    Archives for the category: Fisheye/Crucible Introducing FishEye and Crucible 3.0 – Search, visualize ...

  7. 使用Func<T1, T2, TResult>

    使用Func<T1, T2, TResult> 委托返回匿名对象   Func<T1, T2, TResult> 委托 封装一个具有两个参数并返回 TResult 参数指定的类 ...

  8. 10169 - Urn-ball Probabilities !

    描述:有两个罐子,一个罐子里有一个红球,另一个罐子里有一个红球和一个白球,每次从两个罐子里各取一个球,然后在向每个罐子里各加一个白球,一次取到两个红球时就结束取球,求n次取球中至少一次为取球为红球的概 ...

  9. There is no getter for property named 'userSpAndSp' in 'class com.uauth.beans.UserSpAndSp'

    mybatis 报错There is no getter for property named 'userSpAndSp' in 'class com.uauth.beans.UserSpAndSp' ...

  10. 分享下mac安装xamarin跨平台开发环境的坑

    之前在vs2015上安装好了xamarin环境,考虑到调试IOS仍然需要mac机,昨天决定直接在mac上安装xamarin. 安装完所有的效果如上图,此时已经可以创建安卓和IOS环境. 我安装过程中, ...