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这些比赛的结果能够推导出所有人之间的实力大小 [题解] 如果关系越多.那么就越能确定所有人之间的大小关系. (多一点也能唯一 ...
随机推荐
- java Web中页面跳转方式之重定向和请求转发的区别
请求转发: request.getRequestDispatcher().forward(); 重定向: response.sendRedirect(); 例如: 请求转发: request.getR ...
- node.js中模块报错【window is not defined】的解决方法
(function(window) { /* Keep source code the same */ // })(typeof window == "undefined" ? g ...
- 605. Can Place Flowers零一间隔种花
[抄题]: Suppose you have a long flowerbed in which some of the plots are planted and some are not. How ...
- Cunit编译安装
1. Examples/Makefile.am:26: to 'configure.ac' and run 'autoconf' again. configure.ac:211: error: re ...
- pandas sort_values 排序后, index 也发生了改变,不改变的情况下需要 reset_index(drop = True)
shenpi.sort_values(by=['apply_date'],ascending=True,inplace=True)shenpi.reset_index(drop = True)
- 修改laravel中的pagination的样式
运行如下命令,拷贝出pagination样式到public/vendor目录下, 然后在pagination实例上调用links(‘传路径’)方法 使用起来非常方便,同时也可以自定义样式
- npm 升级
当我们运行某个项目是 会提示 > my-first-vue-project@1.0.0 dev C:\Users\ASUS\my-project > node build/dev-serv ...
- IIS关闭Trace、OPTIONS方法
方法(1):web.config 在<configuration>节点下添加如下代码: <system.webServer> <security> <requ ...
- PC/APP/H5三端测试的相同与不同
随着手机应用的不断状态,同一款产品的移动端应用市场占相较PC端也越来越大,那么app与PC端针对这些产品的测试有什么相同与不同之处呢?总结如下: 首先谈一谈相同之处: 一,针对同一个系统功能的测试,三 ...
- web大文件上传控件-监控f_create流程-Xproer.HttpUploader6
监控f_create流程 1.打开ie,f12 2.启动网络监控 点击开始捕获 上传文件,然后查看监控 将监控信息转到详细视图 向f_create提交的数据 f_create返回值