645D. Robot Rapping Results Report

题目点击打开链接

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 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.

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.

【题意】:

给出一个有向图,确定最小的k,通过前k条边就能确定唯一的拓扑序列。若整个图不存在唯一的拓扑序列,输出-1

【分析】

跑一遍拓扑排序,判断是否有唯一拓扑序列,同时记录这个拓扑序列用到的编号最大的边

//#include<bits/stdc++.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include<cmath>
#define maxn 100005
using namespace std;
using namespace std;
const int N=202020;
struct node{int u,v,next;
}e[N];
int head[N],cnt;
int n,m;
int q[N];
int in[N];
//void add(int u,int v)
//{
//  cout<<"cnt="<<cnt<<endl;
//    //e[cnt]=node{u,v,head[u]};
//    e[cnt].u=u;
//  e[cnt].v=v;
//  e[cnt].next=head[u]; cout<<"e[cnt].u="<<e[cnt].u<<"e[cnt].v="<<e[cnt].v<<"e[cnt].next="<<e[cnt].next<<endl;
//    head[u]=cnt++;cout<<"head[u]="<<head[u]<<endl;
//}
int main()
{while(cin>>n>>m){memset(head,-1,sizeof(head));cnt=0;memset(in,0,sizeof(in));for(int i=0;i<m;i++){int u,v;scanf("%d%d",&u,&v);
//            add(u,v);e[cnt].u=u;e[cnt].v=v;e[cnt].next=head[u]; head[u]=cnt++;in[v]++;}int ans=0;int tail=0,flag=1;for(int i=1;i<=n;i++)if(in[i]==0)q[tail++]=i;if(tail>1)flag=0;for(int i=0;i<tail;i++){int t=q[i];int res=0;for(int j=head[t];~j;j=e[j].next)//注意~j{//cout<<"j="<<j<<endl;if(--in[e[j].v]==0){q[tail++]=e[j].v;//cout<<"e[j].v="<<e[j].v<<endl;res++;ans=max(ans,j); //记录编号最大边}//    cout<<"e[j].next="<<e[j].next<<"res="<<res<<endl;}if(res>1)flag=0;}if(flag==0||tail<n)cout<<-1<<endl;  //不唯一else  {cout<<ans+1<<endl;    //拓扑唯一}
}
return 0;
}

cf-645D. Robot Rapping Results Report(拓扑序列)相关推荐

  1. sdut 2140 有向图中是否存在拓扑序列的判断

    Problem Description 给定一个有向图,判断该有向图是否存在一个合法的拓扑序列. 讲解:http://blog.csdn.net/dm_vincent/article/details/ ...

  2. 数据结构实验之图论十:判断给定图是否存在合法拓扑序列

    Description 给定一个有向图,判断该有向图是否存在一个合法的拓扑序列. Input 输入包含多组,每组格式如下. 第一行包含两个整数n,m,分别代表该有向图的顶点数和边数.(n<=10 ...

  3. nyoj496巡回赛-拓扑排序-拓扑序列

    巡回赛 时间限制:1000 ms | 难度:3 描述 N 名拳击手进行了 M 场比赛,每场赛均可分出胜负,赛后对 N 名选手进行排序,对于每名拳手,必须满足该拳手所战胜过的对手全部排在其后才能对该排名 ...

  4. 图结构练习——判断给定图是否存在合法拓扑序列

    题目描述 给定一个有向图,判断该有向图是否存在一个合法的拓扑序列. 输入 输入包含多组,每组格式如下. 第一行包含两个整数n,m,分别代表该有向图的顶点数和边数.(n<=10) 后面m行每行两个 ...

  5. AcWing 848. 有向图的拓扑序列(拓扑排序模板)

    题面链接 https://www.acwing.com/problem/content/850/ 思路 对于一个有向图来说,只有无环有向图才有拓扑序列,也可以说一个无环有向图必然有拓扑序列,但是不唯一 ...

  6. 有向无环图—拓扑序列

    拓扑序列是对有向无环图的节点编号的排序使得满足任意一条边的起点都在终点的前面,例如: 1->2, 3->2 的有向无环图的一个拓扑序列为1,3,2. 任何一个有向无环图都至少有一个拓扑序列 ...

  7. 拓扑排序【Kahn算法(bfs)和dfs求拓扑序列及判环】

    拓扑排序 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,该排序满足这样的条件--对于图中的任意两个结点u和v,若存在一条有 ...

  8. 使用邻接表输出所有拓扑序列(新手必看!通俗易懂!绝对详细!!)

    最近写了个作业,需求是"输出有向无环图的所有拓扑序列".经过一番折腾,终于完成.之后回想起来觉得应该写一份简单易懂的文章,帮助其他人理解. 细细分析一下,可以发现这次任务我们需要解 ...

  9. AOV网络、拓扑排序、拓扑序列

    AOV网络 AOV网是有向图的一类应用,在AOV网中,用顶点表示某个有一定规模的"工程"里的不同活动,用图中的边表示各项活动之间的先后顺序关系.一种常见的AOV网实例是大学课程的先 ...

  10. 图结构练习——判断给定图是否存在合法拓扑序列(topo)

    图结构练习--判断给定图是否存在合法拓扑序列 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description ...

最新文章

  1. 为什么用U盘做启动盘
  2. Jenkins安装入门
  3. python控制语句第一章_python基础第一章
  4. 简洁自适应个人码农主页源码
  5. 推荐一个非常好玩的falsh游戏
  6. 特斯拉召回部分进口Model S、Model X电动汽车
  7. golang 读取Excel 或者map字符型返回结构体数组
  8. java前缀表达式二叉树课程设计_表达式构建二叉树(中缀,前缀,后缀)
  9. 微信群越来越多,我该如何科学地管理?
  10. HTML制作简单课程表
  11. 用c++编写别踩白块儿小游戏
  12. panabit之Web认证
  13. 如何成为前端开发工程师
  14. GLSL 参考GIMP源码实现色彩平衡调节
  15. zabbix部署+grafana7.2采集数据(时下新版)
  16. c语言程序设计教程+西安交通大学,大学C程序设计教程-西安交通大学.ppt
  17. ae打开模板显示不出来_打开AE模板提示缺少rsmb pro插件的解决办法
  18. matlab 获取雅虎数据,Get Yahoo Finance API Data via YQL,通过YQL获取雅虎财经API数据 - 小众知识...
  19. 关于指数运算符 **
  20. 黑马毕向东Java课程笔记(day16-1-16-9):集合类(集合框架)——Map集合

热门文章

  1. 防抖与节流的原理、实现及优化
  2. C语言:带你轻松干掉 腾讯笔试大题 带环链表
  3. 网络安全笔记2——单钥密码体制
  4. matlab中subs赋值范围,[转载]Matlab的accumarray(subs, val) 解释
  5. 数据结构期末复习速成
  6. 临床数据库挖掘系列3-手把手教你使用R语言对seer数据库清洗
  7. <a>标签下载excel文件
  8. spring boot中mybatisPlus代码生成器源码
  9. golang幽灵蛛(pholcus)(一)
  10. 【Python笔记】Pandas时区处理