CodeForces - 645D A - Robot Rapping Results Report

博客围绕机器人说唱比赛结果展开,已知机器人按确定性算法比赛,能力高者获胜。给出机器人数量、比赛组数及结果,要求确定最少知道前多少组比赛结果能得到唯一的拓扑排序,若无法确定则输出 -1,并给出了分析思路和代码。

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.

题意:给你n个机器人和m组比较,每组的a和b表示a的能力比b强,问你最少知道前多少组可以确定一个唯一的拓扑排序,如果无法找到满足的结果输出-1.

分析:拓扑排序每次只能找到一个能力最大的,还要n个都要找出来,然后就是求用到了第几组。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int frist[100010],book[100010],a[100010];
struct node
{
    int x,y,next;
} q[100010];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(frist,-1,sizeof(frist));
        memset(book,0,sizeof(book));
        memset(a,0,sizeof(a));
        int sum=0,flag=1,num=0,maxx=-0x3f3f3f3f;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&q[i].x,&q[i].y);
            q[i].next=frist[q[i].x];  //邻接表储存 表示a到b的一条有向边
            frist[q[i].x]=i;
            book[q[i].y]++;
        }
        for(int i=1; i<=n; i++)
            if(!book[i])
                a[sum++]=i;   //找能力最大的
        if(sum!=1)
            flag=0;           //如果能力最大的有多个 拓扑排序不唯一
        for(int i=0;i<sum;i++)
        {
            num=0;
            for(int j=frist[a[i]];j!=-1;j=q[j].next)
            {
                book[q[j].y]--;         //已知b[i]的排序
                if(book[q[j].y]==0)     //q[j].y为当前能力最大的
                {
                    a[sum++]=q[j].y;    //入队
                    num++;//当前能力最大
                    maxx=max(maxx,j);   //记录已用到的组数
                }
            }
            if(num>1) flag=0;          //拓扑排序不唯一
        }
        if(!flag||sum<n) printf("-1\n");
        else printf("%d\n",maxx);
    }
}

 

内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值