(CROC 2016 - Elimination Round (Rated Unofficial Edition))D. Robot Rapping Results Report(二分+拓扑排序)
来源:互联网 发布:mysql主从好处 编辑:程序博客网 时间:2024/04/30 15:41
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.
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 52 11 32 34 24 3
4
3 21 23 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场比赛
m场比赛描述是:A打败了B
如果A打败B,B打败C,那么A就能打败C。
现在问你,最少只需要前多少场比赛就能够知道顺序了。
或者说无论如何都不能知道,输出-1
题解:
二分,然后check的时候,检查它的拓扑序是否是一条直线。
#include<bits/stdc++.h>using namespace std;const int maxn = 1e5+7;vector<int>E[maxn];int n,m,E1[maxn],E2[maxn];int in[maxn];bool check(int mid){ memset(in,0,sizeof(in)); for(int i=0;i<=n;i++) E[i].clear(); for(int i=1;i<=mid;i++) E[E1[i]].push_back(E2[i]),in[E2[i]]++; queue<int>Q; for(int i=1;i<=n;i++) if(in[i]==0) Q.push(i); while(!Q.empty()) { int now = Q.front(); Q.pop(); if(Q.size()) return false; for(int i=0;i<E[now].size();i++) { int v = E[now][i]; in[v]--; if(in[v]==0)Q.push(v); } } return true;}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) scanf("%d%d",&E1[i],&E2[i]); int l=1,r=m,ans=-1; while(l<=r) { int mid=(l+r)/2; if(check(mid))r=mid-1,ans=mid; else l=mid+1; } printf("%d\n",ans);}
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))D. Robot Rapping Results Report(二分+拓扑排序)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))C. Enduring Exodus(二分)
- 【CROC 2016 - Elimination RoundD】【二分答案orSet】Robot Rapping Results Report 最早多条边出现可以完成唯一拓扑序
- CodeForces 645D Robot Rapping Results Report(二分+拓扑排序)
- Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))B. Mischievous Mess Makers(贪心)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))A. Amity Assessment(贪心)
- CROC 2016 - Elimination Round (Rated Unofficial Edition)题解
- CodeForces 645 D.Robot Rapping Results Report(二分+dfs)
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E. Intellectual Inquiry 贪心 构造 dp
- CROC 2016 - Elimination Round D 二分+拓扑序+bfs
- Codeforces 645D Robot Rapping Results Report 【dp】
- codeforces 645 D Robot Rapping Results Report 【树形dp】
- CROC 2016 - Elimination Round(A)bfs
- CROC 2016 - Elimination Round C 二分
- Codeforces CROC 2016 - Elimination Round
- Codeforces Croc Champ 2012 - Round 2 (Unofficial Div. 2 Edition)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC) D题
- 快速求解1~n的每个数字出现的次数.
- java工厂模式
- hdu 1069 Monkey and Banana(基础dp)
- Java并发编程:Callable、Future和FutureTask
- POJ 1149
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))D. Robot Rapping Results Report(二分+拓扑排序)
- android学习之fragment
- Java socket报文通信(一)socket的建立
- 羞愧的一天
- 合并排序数组
- TreeMap总结
- Linux网络基础-IP、子网掩码、端口号、DNS、网关
- SQL
- 第四章 Controller接口控制器详解(1)