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这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...
随机推荐
- copy指定目录下包括子目录中所有的文件
#include <windows.h> #include <iostream> #include <string> using namespace std; DW ...
- Asp.net MVC4 CodeFirst 使用EFTracingProvider
一.关于EFTracingProvider EFTracingProvider相关信息见作者博客:关于EFTracingProvider EFTracingProvider Demo下载地址:Trac ...
- [转]iOS Tutorial – Dumping the Application Memory Part 2
Source:https://blog.netspi.com/ios-tutorial-dumping-the-application-memory-part-2/ In my previous bl ...
- [google面试CTCI] 2-0.链表的创建
创建链表.往链表中插入数据.删除数据等操作,以单链表为例. 1.使用C语言创建一个链表: typedef struct nd{ int data; struct nd* next; } node; / ...
- IOS基础开发一(ios程序运行过程)
今天做了一个简单的仿QQ登陆UI晒一晒:
- 序列化json对象,通过ajax传入asp.net mvc后台
序列化json对象,通过ajax传入asp.net mvc后台 序列化json对象,通过ajax传入asp.net mvc后台 今天遇到一个问题,准备把组织好的json对象通过jquery.aja ...
- YSlow的性能测试提示
Add an Expires or a Cache-Control Header tag: server There are two aspects to this rule: For static ...
- Oracle修改字段类型和长度
Oracle修改字段名 alter table 表名 rename column 旧字段名 to 新字段名 Oracle修改字段类型和长度 alter table 表名 modify 字段名 数据类型 ...
- K-Means算法
K-Means算法 K-Means算法的输入N,K和一个size为N的向量组vector.输出K个两两互不相交的向量组.其本质是将给定的向量组划分成K个类别,使得同类别的向量相似度比较大,而不同类别的 ...
- Fast Token Replacement in C#
http://www.codeproject.com/Articles/298519/Fast-Token-Replacement-in-Csharp Fast Token Replacement i ...