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这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...
随机推荐
- 也来“玩”Metro UI之磁贴
也来“玩”Metro UI之磁贴 Win8出来已有一段时间了,个人是比较喜欢Metro UI的.一直以来想用Metro UI来做个自己的博客,只是都没有空闲~~今天心血来潮,突然想自己弄一个磁贴玩玩, ...
- 新版C#编译器关于函数闭包
新版C#编译器关于函数闭包的一处更改 在Visual Basic.NET中,如果你写下类似下面的代码: Public Sub Test() For i = 0 To 100 Dim func = ...
- Arduino 各种模块篇 粉尘传感器 dust sensor 空气质量检测
Testing a sensor from here. http://www.seeedstudio.com/wiki/Grove_-_Dust_Sensor It's a dust sensor. ...
- java动态加载配置文件
最近项目中需要做定时任务,即定时数据库的备份.定时时间用户可以在界面中配置,要求配置修改好立即生效. 想不到什么好办法.下面是一种实现思路 把用户配置的时间存到properties配置文件中,定时任务 ...
- Coding the Matrix作业Python Lab及提交方法
Coding the Matrix: Linear Algebra through Computer Science Applications 这是一门用python实现矩阵运算的课,第一次作业就感觉 ...
- Android开发(22)--seekBar采用handler消息处理操作
本案例简单实现进度条可走,可拖拽的功能,下面请看源码: 布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/ ...
- EL表达式学习
EL表达式取值 1.EL表达式的语法格式很简单: 以前编写jsp代码时,如果要获取表单中的用户名,一般使用 <%=request.getParameter("name")%& ...
- 使用 IDEA 创建 Maven Web 项目 (异常)- Disconnected from the target VM, address: '127.0.0.1:59770', transport: 'socket'
运行环境: JDK 版本:1.8 Maven 版本:apache-maven-3.3.3 IDEA 版本:14 maven-jetty-plugin 配置: <plugin> <gr ...
- Python版C语言词法分析器
#!/usr/bin/python # -*- coding: utf-8 -*- import sys from Tkinter import * from tkFont import * from ...
- 部署 instance 到 OVS vlan100 - 每天5分钟玩转 OpenStack(138)
上一节创建了 OVS vlan network vlan100,今天部署 instance 到该网络.launch 新的 instance “cirros-vm1”,网络选择 vlan100. cir ...