当前位置:网站首页>Robot Rapping Results Report
Robot Rapping Results Report
2022-06-28 08:31: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.
The question : The first line gives n,m, Next there is m Two numbers per line per line u,v, representative u Can win v, ask k The group relationship can determine the unique topological sequence , Question does not exist wei' Unique topology sequence output -1.
Ideas : The idea of this problem is topological sorting , But be careful to record the side with the largest number , And when the number of times lost is 0 Is not unique because of the output -1, While running the topology sorting, record the side with the largest number , Pay attention to the output of special cases . The code is as follows :
#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]++; // Record the number of times
}
int ans=0;
int tail=0,flag=1;
for(int i=1; i<=n; i++) // A topological sort
{
if(s[i]==0)
q[tail++]=i;
}
if(tail>1)flag=0; // The value of 0 There is only one point ,flag=0 Mark the output -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) // Pay attention to j!=-1
{
s[e[j].v]--;
if(s[e[j].v]==0)
{
q[tail++]=e[j].v;
num++;
ans=max(ans,j); // Record number maximum side
}
}
if(num>1)
flag=0;// The value of 0 There is only one point ,flag=0 Mark the output -1
}
if(flag==0||tail<n)
printf("-1\n"); // Is not the only
else
printf("%d\n",ans+1);// Topology is unique
}
return 0;
}
边栏推荐
- Cloudcompare & PCL point cloud clipping (based on closed surfaces or polygons)
- About using font icons in placeholder
- Not so Mobile
- TCP那点事
- B_ QuRT_ User_ Guide(26)
- Introduction, compilation, installation and deployment of Doris learning notes
- 2022巴黎时装周儿童单元6.19武汉站圆满落幕
- Installing mysql5.7 under Windows
- CloudCompare&PCL 点云裁剪(基于封闭曲面或多边形)
- 【学习笔记】差分约束
猜你喜欢

Discussion on the application of GIS 3D system in mining industry

After installing NRM, the internal/validators js:124 throw new ERR_ INVALID_ ARG_ TYPE(name, ‘string‘, value)

Devops Basics: Jenkins deployment and use (I)

利尔达低代码数据大屏,铲平数据应用开发门槛

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

WasmEdge 0.10.0 发布!全新的插件扩展机制、Socket API 增强、LLVM 14 支持

Usage record of Xintang nuc980: self made development board (based on nuc980dk61yc)

关于如何在placeholder中使用字体图标

The preliminary round of the sixth season of 2022 perfect children's model Foshan competition area came to a successful conclusion

AI首席架构师8-AICA-高翔 《深入理解和实践飞桨2.0》
随机推荐
PLSQL installation under Windows
Oracle view tablespace usage
Sword finger offer 30 Stack containing min function
Loss loss function
duilib 入门基础十二 样式类
匿名页的反向映射
Redis deployment under Linux & redis startup
Resolution of Rac grid failure to start after server restart
Unity 获取当前物体正前方,一定角度、距离的坐标点
Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19
Unity - use of API related to Pico development input system ---c
Understanding of CUDA, cudnn and tensorrt
Installing MySQL under Linux
AWS builds a virtual infrastructure including servers and networks (2)
Doris学习笔记之介绍、编译安装与部署
Chenglian premium products donated love materials for flood fighting and disaster relief to Yingde
Unity - Pico开发 输入系统等相关API的使用---C#篇
NPM clean cache
[learning notes] matroid
js运算符的优先级