K. King's Rout
time limit per test

4.0 s

memory limit per test

512 MB

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 aibi (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.

Examples
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 
Note

In the first sample all the permutations where guest number 1 comes after guest number 3 are acceptable according to etiquette. As the valet wants the guest number 1 to come as early as possible he puts him on the second slot and the guest number 3 on the first slot. There is only one slot remaining for the guest number 2.

题意 n个点 m条有向边  输出拓扑排序(编号小的点尽量往前)

解析 我们正常跑拓扑排序时 优先队列并不能解决这个问题 但是我们建个反图  把最大点先弹出来 然后倒序输出 就是正解了

AC代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <set>
using namespace std;
const int maxn = 2e5+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
const double pi = acos(-1.0);
vector<int> g[maxn];
set<int> s[maxn];
int rudu[maxn],order[maxn],vis[maxn];
int n,m,cnt;
void toposort()
{
priority_queue<int,vector<int>,less<int> > q;
for(int i=;i<=n;i++)
if(rudu[i]==)
q.push(i),vis[i]=;
while(!q.empty())
{
int v=q.top();q.pop();
order[cnt++]=v;
for(int i=;i<g[v].size();i++)
{
int u=g[v][i];
rudu[u]--;
if(rudu[u]==)
q.push(u),vis[u]=;
}
}
}
int main()
{
cin>>n>>m;
memset(rudu,,sizeof(rudu));
memset(order,,sizeof(order));
memset(vis,,sizeof(vis));
for(int i=;i<m;i++)
{
int u,v;
cin>>u>>v;
if(s[v].count(v)==)
continue;
else
g[v].push_back(u),s[v].insert(u),rudu[u]++;
}
cnt=;toposort();
for(int i=n-;i>=;i--)
{
if(i==)
cout<<order[i]<<endl;
else
cout<<order[i]<<" ";
}
}

Gym 100792 King's Rout 拓扑排序的更多相关文章

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

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

  2. hdu-5695 Gym Class(贪心+拓扑排序)

    题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  3. HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. 2016 百度之星初赛 Gym Class(优先队列+拓扑排序)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  5. 【拓扑排序】【线段树】Gym - 101102K - Topological Sort

    Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...

  6. 【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs' Box Boutique

    给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排 ...

  7. HDU - 5695 Gym Class 【拓扑排序】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...

  8. Gym 100512G Grand Tour (拓扑排序)

    题意:一个团队要去参观一些学校,某些学校要在某些学校之前先参观,并且每个学校有一个权值,团队去的时间与权值的差作为难过度(最小是0), 所有的难过度的最大值是伤心度,让你安排参观顺序,使得这个伤心度最 ...

  9. 2016"百度之星" - 初赛(Astar Round2A)1006 Gym Class(HDU5695)——贪心+拓扑排序

    分析:首先,利用贪心可知,如果要所有人的分数和最高,需要把序号大的优先放在前面.其次,对于a的前面不能为b,那么只能a在b前面了,那么就建立一条从a到b的边,并且b的入度加1.然后就是拓扑排序了.要分 ...

随机推荐

  1. IO流的原理和概念

    在程序中如何读写文件?不同的编程语言有不同的方式,而 JAVA 则提出了“流”的概念,通过“流”来读写文件 什么是流: 流(Stream)是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通 ...

  2. Glide清除缓存

    Glide是谷歌推荐的一款加载图片的第三方框架,对内存优化更好,更省资源,他的众多好处,我就不一一描述了,有兴趣的朋友可以百度一下,介绍的还是挺详细的. 今天主要给大家介绍一下关于怎么获取Glide的 ...

  3. getDate() 各种时间格式

    Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AMSelect CONVERT(varchar(100), GETDATE() ...

  4. day21-4 菱形继承问题(类的查找顺序)

    目录 菱形继承问题 经典类(了解) 新式类 mro方法 菱形继承问题 在Python中子类可以同时继承多个父类,如A(B,C,D) 如果继承关系为非菱形结构,则会按照先找B这一条分支,然后再找C这条分 ...

  5. selenium兼容非标准chrome内核的浏览器

    多浏览器兼容性测试(1) RIDE已经支持多浏览器兼容性测试,例如: firefox ie chrome safari 但是,项目要求支持360极速和360安全浏览器.所以,我们需要增加代码让RIDE ...

  6. ssd运行过程中遇到的bug

    1.出现以下错误: 没有添加环境变量: https://github.com/weiliu89/caffe/issues/4 可以看到当前PYTHONPATH不再ssd1里面,所以需要修改,修改之后就 ...

  7. C++ 线程同步的四种方式

    程之间通信的两个基本问题是互斥和同步. (1)线程同步是指线程之间所具有的一种制约关系,一个线程的执行依赖另一个线程的消息,当它没有得到另一个线程的消息时应等待,直到消息到达时才被唤醒. (2)线程互 ...

  8. 快速创建你xmlhttp的方法

    function initxmlhttp() {     var xmlhttp     try {         xmlhttp=new ActiveXObject("Msxml2.XM ...

  9. 关于OpenMP的归约操作reduction

    这里提一个重要的点 像这样 ; void ff() { sum += 0.5; } //main() #pragma omp parallel for reduction(+:sum) ; i < ...

  10. 小a与"204"(牛客)

    原题 公式 中间数字与变量之间乘号bug省略可能看着有点别扭例如8x2为8*x2 首先设扫一遍后0的个数为x0 2的个数为x2 4的个数为x4 ①如果x0=x4 ans=32*x4+4 ②如果x0&g ...