codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)

题目链接:

D. Robot Rapping Results Report

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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 ≤ nui ≠ 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 52 11 32 34 24 3

output

4

input

3 21 23 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.

题意:问给你一些数对,第一个数在第二个数的前边,问是否能确定唯一的拓扑序,如果能问前多少数对就能确定,不能输出-1;

思路:把图和每个点的入度存好后就找入度为0的点,再把与这个点能到其他的点的入度-1;一直到所有点的入度都为0,可以用队列存入度为0的点进行优化;如果队列的元素多于1个就输出-1;还有就是把两个拓扑序相邻的点的这条边存起来,最后遍历一遍输入的信息,恰好把这些边减完就是最少的数对数量了;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+4;
int a[N],b[N],n,m,ind[N],mp[N];
queue<int>qu;
vector<int>v[N];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        //mp[a[i]][b[i]]=1;
        ind[b[i]]++;
        v[a[i]].push_back(b[i]);
    }
    int num=0,cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(!ind[i])
        {
            num++;
            qu.push(i);
        }
    }
    while(!qu.empty())
    {
        if(num>1)
        {
            cout<<"-1";
            return 0;
        }
        int x=qu.front();
        qu.pop();
        num--;
        int len=v[x].size();
        for(int j=0;j<len;j++)
        {
            int t=v[x][j];
            ind[t]--;
            if(!ind[t])
            {
                qu.push(t);
                num++;
                mp[t]=x;
                cnt++;
            }
        }
    }
    for(int i=1;i<=m;i++)
    {
        if(mp[b[i]]==a[i])
           {
               cnt--;
               if(cnt==0)
               {
                   cout<<i<<"\n";
                   break;
               }
           }

    }

    return 0;
}
时间: 2024-08-27 04:33:27

codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)的相关文章

CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是否所有关系被唯一确定.即任意一次只能有1个元素入度为0入队. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <string> 5 #include <

CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 拓扑排序+二分

题目链接: http://www.codeforces.com/contest/655/problem/D 题意: 题目是要求前k个场次就能确定唯一的拓扑序,求满足条件的最小k. 题解: 二分k的取值,做拓扑排序的时候只要每次只有一个元素没有前驱就可以唯一了. #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #includ

codeforces 645D Robot Rapping Results Report

二分一下答案check即可.(拓扑排序) #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #define maxv 100500 #define maxe 200500 using namespace std; int n,m,nume=0,g[maxv],x[maxv],y[maxv],d[maxv],rank[m

hdu 4857 逃生(拓扑排序逆序 + 优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题意:有编号 1 - n 的 n 个人逃生,编号越小的人越有钱, 在满足 m 个前提的条件下要尽可能早的逃脱 .m个前提,对于每个前提 a , b,代表 a 要早于 b逃脱. 思路: (1)这题可以理解为有钱的人优先级越高,所以可以用优先队列. (2)但是要注意这道题和字典序升序的区别. eg:5 1 5 1 按照字典序的答案:2 3 4 5 1,    本题答案: 5 1 2 3 4. 因为

[2016-02-18][拓扑排序]

[2016-02-18][拓扑排序] 拓扑排序 思路    dfs 分析 从树的角度来看,就是不同层的节点,顺序是一定的,同层的节点,顺序是不定的, 但是拓扑排序的图不一定是树,可以有环(不能是同向的环,即1->2->3->1 这种) 为了避免漏了节点,必须枚举每一个点跑一次dfs 为了避免出现通向环的情况,必须加上标志,在每次dfs时候,标记非0值表示访问过,但是进入下一层dfs时,标记为-1,如果下层dfs访问到这个节点时的标记为-1,说明有环,出来dfs才设置为1,表示这个点后面没

数据结构:图--拓扑排序

拓扑排序 拓扑排序 在实际应用中,有向图的边可以看做是顶点之间制约关系的描述.把顶点看作是一个个任务,则对于有向边<Vi,Vj>表明任务Vj的完成需等到任务Vi完成之后,也就是说任务Vi先于任务Vj完成.对于一个有向图,找出一个顶点序列,且序列满足:若顶点Vi和Vj之间有一条边<Vi,Vj>,则在此序列中顶点Vi必在顶点Vj之前.这样的一个序列就称为有向图的拓扑序列(topological order). 步骤 从有向图中选取一个没有前驱(入度为0)的顶点输出. 删除图中所有以它为

数据结构 - 拓扑排序

应用背景 学生选修课程问题 顶点--表示课程 有向弧--表示先决条件,若课程i是课程j的先决条件,则图中有弧(i,j) 学生应按怎样的顺序学习这些课程,才能无矛盾.顺利地完成学业--拓扑排序 拓扑序列是有向无环图中各顶点构成的有序序列.该序列满足如下条件:如果图中一顶点vi到另一顶点vj存在一条路径,那么vj在此图的拓扑排序序列中位于vi之后. 有向无环图(DAG)和 AOV网 有向无环图"(Directed Acyclic Graph,简称DAG AOV网 (Activity On Verte

算法-有向环和拓扑排序

有向图中包括有向无环图和有向有环图,有向图在任务调度的时候优先级限制是非常有用的,最常见的是大学的排课系统,比如说计算机操作系统的优先级高于高等数学,我们可以用图表示为计算机操作系统→高等数学,高等数学高于线性代数,如果这个时候线性代数的优先级高于计算机操作系统,那么就产生了一个有向环,无法进行排课,课程一般比较多,如果用图表示去判断是否存在环路是比较麻烦的一个事情,所以通常需要判断有向图中是否含有向环. 有向环检测 如果有向图中的某个节点可以按照路径的方向从某个节点开始并返回本身,形成了闭环可

hihoCoder#1185 : 连通性&#183;三 tarjan求强联通分量 缩点 dfs/拓扑排序求路径和最大值

题目链接: http://hihocoder.com/problemset/problem/1185# 题意: n个点,每个点有一个权值,m条有向边,从1出发,每走到一个点, 就吃掉这个点的草,当没有可以到达的草场或是能够到达的草场都已经被吃光了之后就要返回到1了.求最多可以吃掉多少草. 思路: 提示里面讲的挺好的 如果草场是一个强连通图,那么我们只要走到任意一点,就可以把其他所有的草场都走一遍,并且可以选择任意一个点作为终点.所以把强联通块缩成一个点 因为一个强连通块会被缩成一个点,那么我们可