CROC 2016 - Elimination Round D 二分+拓扑序+bfs



链接:戳这里


D. Robot Rapping Results Report
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.


题意:给出n个节点m条边(2<=n<=1e5)( 1<=m<=min(1e5,n*(n-1)/2) ),接下来m条边u,v表示  u->v  节点u的等级比v高

问最少需要前多少条边可以确定所有的节点的等级(每个节点的等级唯一,不会出现重边)


思路:我们需要前ans条边来确定图的拓扑序唯一,首先肯定是二分这个答案啊

然后在 bfs+队列 判断当前入度为0的节点的个数num,如果num>1则说明当前不能确定唯一的拓扑序,反之则确定


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int U[1000100],V[1000100];
int n,m;
struct edge{
    int v,next;
}e[1000100];
int head[1000100],tot,in[1000100],vis[1000100];
void init(){
    mst(head,-1);
    mst(in,0);
    tot=0;
}
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
bool judge(int x){
    init();
    for(int i=1;i<=x;i++) {
        Add(U[i],V[i]);
        in[V[i]]++;
    }
    queue<int> qu;
    for(int i=1;i<=n;i++){
        if(in[i]==0) qu.push(i);
    }
    while(!qu.empty()){
        int now=qu.front();
        qu.pop();
        if(qu.size()>0) return false;
        for(int i=head[now];i!=-1;i=e[i].next){
            int v=e[i].v;
            in[v]--;
            if(in[v]==0) {
                qu.push(v);
            }
        }
    }
    return true;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d%d",&U[i],&V[i]);
    int l=1,r=m,mid,ans=-1;
    if(l==r && judge(r)) {
        cout<<r<<endl;
        return 0;
    }
    while(l<r){
        mid=(l+r)/2;
        if(judge(mid)) {
            r=mid;
            ans=mid;
        } else l=mid+1;
    }
    if(ans==-1) {
        if(judge(m)) ans=m;
    }
    cout<<ans<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值