zoukankan      html  css  js  c++  java
  • 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 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.

    题意:问给你一些数对,第一个数在第二个数的前边,问是否能确定唯一的拓扑序,如果能问前多少数对就能确定,不能输出-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<<"
    ";
                       break;
                   }
               }
    
        }
    
    
        return 0;
    }
  • 相关阅读:
    public interface IBaseService<T> where T:class, new()含义
    mvc多条件查询
    jquery select下拉框和 easy-ui combox 选定指定项区别
    .net 中主框架的搭建(2种方式)
    linq ->sql & linq->lambda中的cud
    mvc中日志的原理和使用步骤
    Sprint.Net和Mvc结合使用
    2017.5.12总结
    c#前端验证和后台验证总结
    matlab根据url链接下载*.tar文件并解压
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5295054.html
Copyright © 2011-2022 走看看