CROC 2016 - Elimination Round D. 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.
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.
拓扑排序 然后判断是否是唯一的拓扑排序,如果是唯一的话 输出最大的可以边可以满足题意。
链式前向星+拓扑排序
#include<stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include<deque>
#include<map>
#include<set>
#define ll long long
#define INF 0x3f3f3f3f
#define eps 1e-8
const int
mod = 1e9+7;
using namespace std;
int KGCD(int a,int b){if(a==0)return b;if(b==0)return a;if(~a&1){ if(b&1) return KGCD(a>>1,b);else return KGCD(a>>1,b>>1) <<1; } if(~b & 1)
return KGCD(a, b>>1);
if(a > b) return KGCD((a-b)>>1, b);return KGCD((b-a)>>1, a);}
int LCM(int a,int b){ return a/KGCD(a,b)*b; }
inline ll qpow(ll n,ll m){n%=mod;ll ans=1;while(m){if(m%2) ans=(ans*n)%mod;m/=2;n=(n*n)%mod;}return ans;}
inline ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}
inline ll inv2(ll b){return qpow(b,mod-2);}
int dir[5][2]={0,1,0,-1,1,0,-1,0};
struct node{
int u;
int v;
int next;
}nn[100005];
int head[100005];
int in[100005];
int push[100005];
int cnt=0;
int n,m;
void add(int u,int v)
{
nn[cnt].u=u;
nn[cnt].v=v;
nn[cnt].next=head[u];
head[u]=cnt++;
}
int main()
{
int u,v;
int top=0;
int index=0;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
memset(in,0,sizeof(in));
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
in[v]++;
}
for(int i=1;i<=n;i++)
{
if(in[i]==0)
push[top++]=i;
}
if(top>1)
{
printf("-1n");
return 0;
}
else
{
for(int i=0;i<top;i++)
{
int ans=0;
for(int k=head[push[i]];~k;k=nn[k].next)
{
if(--in[nn[k].v]==0)
{
ans++;
push[top++]=nn[k].v;index=max(index,k);
}
}
if(ans>1)
{
printf("-1n");
return 0;
}
}
}
if(top<n)
printf("-1n");
else
printf("%dn",index+1);
return 0;
}
最后
以上就是俊秀胡萝卜最近收集整理的关于CROC 2016 - Elimination Round D. Robot Rapping Results Report的全部内容,更多相关CROC内容请搜索靠谱客的其他文章。
- 本文分类:~~~~~CodeForces~~~~~
- 浏览次数:207 次浏览
- 发布日期:2023-12-24 00:00:22
- 本文链接:https://www.kaopuke.com/article/k-p-k_13_u_23_o_2_f4_14_z_14_z.html
发表评论 取消回复