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 ;
}
最新文章
- Window10+VS2015+DevExpress.net 15.1.7完美破解(图)
- oracle学习笔记系列------oracle 基本操作之基本函数的用法
- Machine Learning for hackers读书笔记(七)优化:密码破译
- JVM --java 字节码的结构解析
- 用户界面线程AfxBeginThread的使用
- Android JNI programming demo with Eclipse
- RunJS推荐用于个人使用(使用方便JS、css实时预览、编辑、管理等功能)
- 使用github出了些问题?fatal: unable to access;Failed connect to github.com:8087;
- 一步一步带你实现virtual dom(一)
- R语言︱词典型情感分析文本操作技巧汇总(打标签、词典与数据匹配等)
- AD服务无法启动
- sjms-2 创建型模式
- FromData获取表单数据
- 慢工出细活 JS 等待加载效果
- sqlserver2008r2安装
- Dell笔记本Ubuntu无线网卡驱动安装
- excel自定义函数添加和使用方法
- Centos6.8下搭建SVN服务器
- uipath接入Python
- python模块之XlsxWriter
热门文章
- 408. Valid Word Abbreviation有效的单词缩写
- SparkR 读取数据&; Spark运行的配置
- Yii2验证登录得User类
- WCF把书读薄(4)——事务编程与可靠会话
- LightOJ 1065 Island of Survival (概率DP?)
- wp7启动+幻灯片效果
- 1235: 入学考试[DP]
- kali linux之搜索引擎Shodan
- kali linux之被动信息收集(dns信息收集,区域传输,字典爆破)
- 使用IIS服务器部署网页,需要开启服务里的“ASP.NET 状态服务”