题目:

K. King’s Rout
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

The great rout will be held this evening in the palace of his majesty Nassah II, the king of Occorom. There are n guests invited. While they are preparing evening dresses and collecting fresh rumors to talk about, the chief valet of the palace has a tricky task to solve: choose the right order for persons to arrive to the palace.

Guests always arrive one by one, that is, no two guests may arrive at the same moment of time. Due to the court etiquette, there are some limitations on the order of the arrival. For example, a notable landlord should arrive later than all his vassals, but should be earlier than his wives. After reading “Etiquette guide for dummies” the valet found out m order conditions to be satisfied. Each of them has a form: ai must come before bi. Rules are so complicated that some conditions may appear in the list two or more times.

So far the problem seems to be easy and familiar. But some guests (actually, all of them) tried to bribe valet to allow them arrive before others. So valet sorted guests according to their payoffs amounts and decided that guest number 1 should arrive as early as possible (without violating etiquette rules), among all such options valet chooses the one with the guest number 2 arriving as early as possible, and so on. All payoffs were different, so valet has no problem in selecting guests priority.

Help valet to find the best possible schedule. Guests already have numbers in valet’s private list of priority, so you will not know bribes amounts and will not be accused in complicity in corruption.
Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 200 000, 0 ≤ m ≤ 400 000) — the number of guests invited and the number of order conditions respectively.

Next m lines describe the conditions, each of them containing a single pair ai, bi (1 ≤ ai, bi ≤ n). That means the guest ai is required to come earlier than the guest bi.
Output

Print n different integers from 1 to n to describe the best possible order (according to valet’s understanding) for guests to arrive. It is guaranteed that at least one valid order exists.
Sample test(s)
input

3 1
3 1

output

3 1 2

input

5 6
2 1
5 2
4 1
5 4
3 1
5 3

output

5 2 3 4 1

思路:

题目中要求对于没有强制性关系的人,要使得序号小的尽量在前边。
比如这组数据:
5 3
5 2
2 1
4 3
答案是 5 2 1 4 3
在正常的拓扑排序时没有那么多限制条件,但在这里,我们发现如果是按照每个点的入度来判断的话,比如刚才举着这个例子:
5(0)→2(1)→(1){5(0)\rightarrow2(1)\rightarrow(1)} // 括号里是入度
4(0)→3(1){4(0)\rightarrow3(1)}
我们可以看到在第一条链中有1,所以第一条链应该在第二条之前,但是这样的话,我们便对先放谁产生了疑问。要这么写的话,估计还得来个一场链的判断,各种复杂度就上去了。
所以我们换个思路:
5(1)→2(1)→(0){5(1)\rightarrow2(1)\rightarrow(0)} // 括号里是出度
4(1)→3(0){4(1)\rightarrow3(0)}
每次把出度为0的放入优先队列,每次从队列中取出最大的那个数,放在答案数组里(注意倒叙放~)。
这样就能保证每次放在后边的总是大的,即满足了强制性的关系,也把自由的点中大的放在后边了。

AC代码:

#include <iostream>
#include <queue>
#include <cstdio>using namespace std;vector<int> dir[200010];
int deep[200010];
priority_queue<int> q;//大的优先级高
int ans[200010];void toposort(int n){int temp;while(!q.empty()){temp = q.top();ans[n--] = temp;q.pop();for(int i = 0;i < dir[temp].size();i++){deep[dir[temp][i]]--;if(deep[dir[temp][i]] == 0) q.push(dir[temp][i]);}}
}int main()
{int n,m;int temp1,temp2;while(~scanf("%d%d",&n,&m)){for(int i = 1;i <= m;i++){scanf("%d%d",&temp1,&temp2);dir[temp2].push_back(temp1);deep[temp1]++;}for(int i = n;i >= 1;i--){if(deep[i] == 0){q.push(i);}}toposort(n);for(int i = 1;i <= n;i++){printf("%d ",ans[i]);}printf("\n");for(int i = 1;i <= n;i++){dir[i].clear();}}return 0;
}

GYM 100792k King's Rout (拓扑排序+优先队列)相关推荐

  1. Gym 100792K King’s Rout

    Gym 100792K King's Rout 拓扑排序 传送门:HustOJ 传送门:CodeForce 题意 有编号为1~n的客人要去吃饭,然后他们会先后到达,现在已经知道了有些人一定会先于另外有 ...

  2. HDU 4857 拓扑排序 优先队列

    n个数,已经有大小关系,现给m个约束,规定a在b之前,剩下的数要尽可能往前移.输出序列 大小关系显然使用拓扑结构,关键在于n个数本身就有大小关系,那么考虑反向建图,优先选择值最大的入度为零的点,这样得 ...

  3. P3243-[HNOI2015]菜肴制作【拓扑排序,优先队列】

    正题 题目链接:https://www.luogu.com.cn/problem/P3243 题目大意 nnn个数,有mmm个要求形如xxx在yyy的前面,现在要求在i−1i-1i−1尽量靠前的情况下 ...

  4. hdu1285 拓扑排序+优先队列

    原题地址 这算是我个人AC的第一个拓扑排序题目吧. 题目解读 给出几组比赛的胜负情况.推断最后的排名.依据题意这就是一个明显的拓扑排序问题了. 注意 假设由于可能的排名有多种情况,这时要保证编号小的在 ...

  5. zcmu-2153(拓扑排序+优先队列)

    2153: D.ly的排队问题 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 35  Solved: 13 [Submit][Status][Web ...

  6. HDU1285确定比赛名次(拓扑排序+优先队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1285 思路 每场比赛的结果我们可以看成一个有向图,从胜利的一方指向失败的一方,在这个有向图的入度为0的 ...

  7. HDU Problem 4857 逃生【拓扑排序+优先队列】

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  8. GYM100792K King‘s rout

    题意: 有n个人,让你给排个序,其中有一些规则,首先要满足对于序对<a,b>,a要在b的前面输出,对于没有标定次序的序号,要满足,越小的序号,越先输出越好: 思路:反向拓扑排序+优先队列 ...

  9. 2016百度之星 - 初赛(Astar Round2A)Gym Class(拓扑排序)

    Gym Class  Accepts: 849  Submissions: 4247  Time Limit: 6000/1000 MS (Java/Others)  Memory Limit: 65 ...

最新文章

  1. 欢迎来到美多商城!-项目准备之项目介绍-项目需求分析-项目架构设计
  2. codeup:问题 D: 最短路径
  3. Navigator 对象 深入研究
  4. VBA在EXCEL中创建图形线条
  5. lightroom安卓_安卓可以用的一款PS
  6. T5: Text-to-Text Transfer Transformer 阅读笔记
  7. android广告平台的介绍
  8. mysql 逆序排序_将一组乱序的字符进行排序进行升序和逆序输出
  9. 这就是Machine Learning
  10. Hive导入csv文件
  11. centos 6.5环境利用iscsi搭建SAN网络存储服务及服务端target和客户端initiator配置详解...
  12. spring事务传递机制原理
  13. 姜启源《数学建模》学习笔记 第一周
  14. UCOSIII实时操作系统
  15. 软件测试笔记本硬件,专业工作站软件测试_惠普笔记本电脑_笔记本评测-中关村在线...
  16. 基于周志华西瓜数据集的决策树算法及准确率测试
  17. 笔记本电脑计计算机硬盘分区,笔记本电脑如何分区,小编教你笔记本电脑如何分区...
  18. 番茄钟工作法:你真的了解番茄钟么?
  19. 使用vscode开发vue项目
  20. win服务器性能测试,windows服务器性能测试

热门文章

  1. js 的一些知识 摘自http://img0.pconline.com.cn/Pc_intranet/1105/13/313647_7.pdf
  2. 如何查看小方侦测云存储_小方智能摄像机云存储 云存储摄像机下载
  3. 如何打开联想计算机主机,联想计算机如何进入BIOS界面
  4. 倒计时效果 2020-11-24
  5. 20230402英语学习
  6. 《Head First HTML with CSS XHTML》要点总结
  7. python 小孩学编程_让小孩学Python语言编程有意义吗?
  8. ppt计算机说课教案,教师资格证面试高中信息技术说课教案:制作幻灯片
  9. 【GStreamer 】5-1 gstreamer实现RTSP相机显示
  10. 一篇很好的文章,学verilog的可以好好看看!(转载)