当前位置:网站首页>Robot Rapping Results Report
Robot Rapping Results Report
2022-06-28 08:10:00 【Angeliaaa】
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level.
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.
Input
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 viin 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.
Output
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.
Examples
Input
4 5 2 1 1 3 2 3 4 2 4 3
Output
4
Input
3 2 1 2 3 2
Output
-1
Note
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,接下来有m行每行每行两个数u,v,代表u能赢v,问k组关系就能确定唯一的拓扑序列,问不存在wei'唯一的拓扑序列输出-1.
思路:这个题的思想是拓扑排序,但注意要记录编号最大的边,并且当输的次数为0的情况不唯一时因该输出-1,跑一边拓扑排序同时记录一下编号最大的边,注意特殊情况输出即可。代码如下:
#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 100005
using namespace std;
using namespace std;
struct node
{
int u,v,next;
} e[100010];
int head[100010],cnt;
int n,m;
int q[100010];
int s[100010];
int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(head,-1,sizeof(head));
cnt=0;
memset(s,0,sizeof(s));
for(int i=0; i<m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
e[cnt].u=u;
e[cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt++;
s[v]++; //记录输的次数
}
int ans=0;
int tail=0,flag=1;
for(int i=1; i<=n; i++) //拓扑排序
{
if(s[i]==0)
q[tail++]=i;
}
if(tail>1)flag=0; //这个值为0的点只有一个,flag=0标记该输出-1
for(int i=0; i<tail; i++)
{
int t=q[i];
int num=0;
for(int j=head[t]; j!=-1; j=e[j].next) //注意就j!=-1
{
s[e[j].v]--;
if(s[e[j].v]==0)
{
q[tail++]=e[j].v;
num++;
ans=max(ans,j); //记录编号最大边
}
}
if(num>1)
flag=0;//这个值为0的点只有一个,flag=0标记该输出-1
}
if(flag==0||tail<n)
printf("-1\n"); //不唯一
else
printf("%d\n",ans+1);//拓扑唯一
}
return 0;
}
边栏推荐
- 整数划分
- 关于如何在placeholder中使用字体图标
- MySQL two table connection principle (understand join buf)
- [learning notes] shortest path + spanning tree
- Estimation of SQL execution cost by MySQL query optimizer
- 【学习笔记】线性基
- 【学习笔记】搜索
- Resolution of Rac grid failure to start after server restart
- Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation
- Eslint syntax monitoring off
猜你喜欢

Doris学习笔记之介绍、编译安装与部署

Installing mysql5.7 under Windows

Unity gets the coordinate point in front of the current object at a certain angle and distance

你了解TCP協議嗎(二)?

Introduction, compilation, installation and deployment of Doris learning notes

【尚品汇】项目笔记

Introduction to Devops Basics

The maximum number of Rac open file descriptors, and the processing of hard check failure

ROS 笔记(09)— 参数的查询和设置

Redis master-slave structure and application scenarios
随机推荐
ROS notes (08) - definition and use of service data
Redis deployment under Linux & redis startup
Build an integrated kubernetes in Fedora
Oracle RAC -- understanding of VIP
关于在cmd中MySQL不能插中文数据的原因
AI首席架构师8-AICA-高翔 《深入理解和实践飞桨2.0》
城联优品向英德捐赠抗洪救灾爱心物资
【学习笔记】模拟
Kubernetes notes and the latest k3s installation introduction
redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
MySQL implements transaction persistence using redo logs
小艺人黄鑫洋受邀参加巴黎时装周儿童单元武汉站
你了解TCP协议吗(二)?
B_ QuRT_ User_ Guide(30)
块级元素上下左右居中的两个小技巧
Eslint 语法监测关闭
On the solution of insufficient swap partition
Set the encoding of CMD to UTF-8
十大券商注册开户靠谱吗?安全吗?
Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)