CodeForces - 645D Robot Rapping Results Report(拓扑排序)

本文探讨了一个问题,即确定最少的比赛次数以便能够明确地按技能水平对参赛机器人进行排序。通过分析比赛结果,利用拓扑排序算法,文章提供了一种有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
Copy
4 5
2 1
1 3
2 3
4 2
4 3
output
Copy
4
input
Copy
3 2
1 2
3 2
output
Copy
-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,代表n个点m条边,下面m行代表u为v的父亲节点,求当恰好给出多少条边时可以得到所有点的次序。

解题思路:在建树时同时记录以下这条边时给出的第几条边,用边权记录。用ans记录答案。进行拓扑排序,拓扑排序时一旦同时出现两个及以上的0入度点,则说明有多个点的次序无法准确排序,那么结果就是-1,否则向队列压入0入度点的同时记录一下ans = max(ans, 边权),整个拓扑排序结束后,这个ans就是所求结果。因为这样就相当于记录了最高次序的那个点所连接的下一个0入度点之间的边权值,也就是最多需要给出的边。

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<string>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 using namespace std;
12 const int MaxN = 1e5;
13 const int Inf = 1 << 30;
14 typedef struct
15 {
16     int to, od;
17 } Power;
18 
19 vector <Power> num[MaxN+5];
20 int n, m, ans;
21 int ind[MaxN+5];
22 
23 bool Toposort()
24 {
25     queue <int> que;
26     for(int i = 1;i <= n;i++)
27     {
28         if(ind[i] == 0) que.push(i);
29     }
30     int u;
31     Power v;
32     while(!que.empty())
33     {
34         u = que.front();
35         que.pop();
36         if(!que.empty()) return 0;
37         for(int i = 0;i < num[u].size();i++)
38         {
39             v = num[u][i];
40             ind[v.to]--;
41             if(!ind[v.to])
42             {
43                 ans = max(ans, v.od);
44                 que.push(v.to);
45             }
46         }
47     }
48     return 1;
49 }
50 
51 int main()
52 {
53     cin >> n >> m;
54     int a, b;
55     Power S;
56     for(int i = 1;i <= m;i++)
57     {
58         scanf("%d %d", &a, &b);
59         S.to = b;S.od = i;
60         num[a].push_back(S);
61         ind[b]++;
62     }
63     if(!Toposort()) printf("-1\n");
64     else printf("%d\n", ans);
65     return 0;
66 }

 

 

转载于:https://www.cnblogs.com/ScaleCX/p/9332690.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值