CodeForces - 645D Robot Rapping Results Report(拓扑排序)
Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level.
The first line of the input consists of two integers, the number of robots n (2 ≤ n ≤ 100 000) and the number of rap battles m ().
The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi), indicating that robot ui beat robot vi in the i-th rap battle. No two rap battles involve the same pair of robots.
It is guaranteed that at least one ordering of the robots satisfies all m relations.
Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1.
4 5
2 1
1 3
2 3
4 2
4 3
4
3 2
1 2
3 2
-1
In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles.
In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles.
题目大意:输入第一行是n和m,代表n个点m条边,下面m行代表u为v的父亲节点,求当恰好给出多少条边时可以得到所有点的次序。
解题思路:在建树时同时记录以下这条边时给出的第几条边,用边权记录。用ans记录答案。进行拓扑排序,拓扑排序时一旦同时出现两个及以上的0入度点,则说明有多个点的次序无法准确排序,那么结果就是-1,否则向队列压入0入度点的同时记录一下ans = max(ans, 边权),整个拓扑排序结束后,这个ans就是所求结果。因为这样就相当于记录了最高次序的那个点所连接的下一个0入度点之间的边权值,也就是最多需要给出的边。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
const int MaxN = 1e5;
const int Inf = << ;
typedef struct
{
int to, od;
} Power; vector <Power> num[MaxN+];
int n, m, ans;
int ind[MaxN+]; bool Toposort()
{
queue <int> que;
for(int i = ;i <= n;i++)
{
if(ind[i] == ) que.push(i);
}
int u;
Power v;
while(!que.empty())
{
u = que.front();
que.pop();
if(!que.empty()) return ;
for(int i = ;i < num[u].size();i++)
{
v = num[u][i];
ind[v.to]--;
if(!ind[v.to])
{
ans = max(ans, v.od);
que.push(v.to);
}
}
}
return ;
} int main()
{
cin >> n >> m;
int a, b;
Power S;
for(int i = ;i <= m;i++)
{
scanf("%d %d", &a, &b);
S.to = b;S.od = i;
num[a].push_back(S);
ind[b]++;
}
if(!Toposort()) printf("-1\n");
else printf("%d\n", ans);
return ;
}
CodeForces - 645D Robot Rapping Results Report(拓扑排序)的更多相关文章
- 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 拓扑排序+二分
题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值 ...
- CodeForces 645D Robot Rapping Results Report
二分,拓扑排序. 二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$0$的节点,那么不可行. #pragma comment(linker, "/STACK:102400 ...
- Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...
- 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 二分+拓扑排序
题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...
- 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这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...
随机推荐
- Mask_RCNN caffe2
cd DETECTRON/detectron/ python2 tools/infer_simple_ip_camera.py \ --cfg configs/12_2017_baselines ...
- 1-new对象与直接构建对象
#include <iostream> using namespace std; class A { public: A(){} A (int a){ this->a = a; } ...
- mybatis学习笔记 spring与mybatis整合
转载自http://blog.csdn.net/naruto_Mr/article/details/48239357 1.创建web工程,导入spring依赖包与mybatis依赖包,还需要mybat ...
- 单机配置tomcat 8 集群
如何能在集群中的多个节点之间保持数据的一致性,会话(Session)信息是这些数据中最重要的一块. 本文当采用tomcat默认集群配置(<Cluster className="org. ...
- 29.MAX() 函数
MAX() 函数 MAX 函数返回一列中的最大值.NULL 值不包括在计算中. SQL MAX() 语法 SELECT MAX(column_name) FROM table_name 注释:MIN ...
- 如果你的资源贫乏,那么专注做好一件事将是你的唯一出路(no reading yet)
http://www.jianshu.com/p/8784f0fd7ab8/comments/1161511
- LoadRunner使用问题
最近给客户做POC,为了测试大数据的框架的一个并发能力,使用loadrunner进行相关的测试,目前发现几个要注意的地方 1: loadrunner的Java脚本必须使用jdk1.6的32位版本 2: ...
- 使用 Vue.component
引入 vue.js. HTML <div id="app"></div> CSS .greeting { padding: 3rem 1.5rem; bac ...
- wp面试题
初级工程师 解释什么是依赖属性,它和以前的属性有什么不同?为什么在WPF会使用它? 什么是样式什么是模板 绑定(Binding )的基础用法 解释这几个类的作用及关系: Visual, UIEleme ...
- 自己总结的,输出到前端JSON的几种方法
第一种:利用MODEL拼成要输出JSON的对象.再用JSON.NET转成JSON输出到前端(这种常用,就不举例了.) 第二种:利用table拼成JSON数据格式,再用JSON.NET转成JSON输出到 ...