CodeForces 645D Robot Rapping Results Report
二分,拓扑排序。
二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$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的更多相关文章
- CodeForces - 645D Robot Rapping Results Report(拓扑排序)
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some a ...
- Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...
- codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)
题目链接: D. Robot Rapping Results Report time limit per test 2 seconds memory limit per test 256 megaby ...
- 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 ...
- codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序
题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分
题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...
- CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序
题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...
- 【CF645D】 Robot Rapping Results Report(拓扑排序,二分)
题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序, 若没有唯一拓扑序输出-1 思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<&g ...
- 【【henuacm2016级暑期训练】动态规划专题 O】Robot Rapping Results Report
[链接] 我是链接,点我呀:) [题意] 让你确定一个最小的k 使得1..k这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...
随机推荐
- MVC几种传值方式
一,Model public class Course { public int Id { get; set; } public string Name { get; set; } } public ...
- Tomcat中Context的配置
Tomcat直接ip地址访问不用加端口和项目名 当我们开发完一个WEB项目 然后部署到tomcat下,正常情况下应该是这样访问:http://localhost:端口号/项目名 如果我们想让用户仅仅输 ...
- PrintWriter返回值乱码问题
⑴response.setCharacterEncoding("utf-8"); ⑵response.setContentType("text/html; charset ...
- js中的“闭包”
js中的“闭包” 姓名:闭包 官方概念:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. ( ⊙o⊙ )!!!这个也太尼玛官方了撒,作为菜鸟的 ...
- 转--htaccess语法教程 apache服务器伪静态规则教程
转自:http://blog.csdn.net/qingli518/article/details/9118361 htaccess语法教程apache服务器伪静态规则教程 注:如果你是为了找Disc ...
- JavaScript的基本类型总结
看了让你有收获的JavaScript的基本类型总结 Javascript是一种弱类型语言,没有明确的类型分类:网上分类的方式比较多,个人感觉不比去特别的追究细分是什么什么类型,若是能够明确的分出类 ...
- WCF客户端与服务端通信简单入门教程
服务端 1.新建空白解决方案,然后再空白解决方案中新建:WCF服务应用程序.建完后如图: 2.删掉自动生成的IService1.cs和Service.svc并添加WCF服务文件StudentServi ...
- 对比AutoResetEvent和ManualResetEvent
ManualResetEvent和AutoResetEvent 比较 ManualResetEvent和AutoResetEvent都继承自EventWaitHandler,它们的唯一区别就在于父类 ...
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic
之前遇到过这样的问题, 04-12 10:40:33.302: E/AndroidRuntime(17213): Caused by: android.view.WindowManager$BadTo ...
- 命令版本git 分支篇-----不断更新中
最近应用开发的过程中出现了一个小问题,顺便记录一下原因和方法--命令版本 开发中想看看过去某个版本的代码,我们先查看log git log commit f224a720b8192165a4e70f2 ...