Gym 100792 King's Rout 拓扑排序

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 13 1

output

3 1 2 

input

5 62 15 24 15 43 15 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+50;
const int maxm = 1e4+10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double epx = 1e-6;
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=1;i<=n;i++)
        if(rudu[i]==0)
            q.push(i),vis[i]=1;
    while(!q.empty())
    {
        int v=q.top();q.pop();
        order[cnt++]=v;
        for(int i=0;i<g[v].size();i++)
        {
            int u=g[v][i];
            rudu[u]--;
            if(rudu[u]==0)
                q.push(u),vis[u]=1;
        }
    }
}
int main()
{
    cin>>n>>m;
    memset(rudu,0,sizeof(rudu));
    memset(order,0,sizeof(order));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<m;i++)
    {
        int u,v;
        cin>>u>>v;
        if(s[v].count(v)==1)
            continue;
        else
            g[v].push_back(u),s[v].insert(u),rudu[u]++;
    }
    cnt=0;toposort();
    for(int i=n-1;i>=0;i--)
    {
        if(i==0)
            cout<<order[i]<<endl;
        else
            cout<<order[i]<<" ";
    }
}

Gym 100792 King's Rout 拓扑排序

原文地址:https://www.cnblogs.com/stranger-/p/8763122.html

时间: 2024-12-15 14:29:44

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

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

题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到N,在排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为

【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs&#39; Box Boutique

给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排序. 如果前面的宽大于等于后面的宽,就连接一条边. 形成一张DAG,拓扑排序后跑最长路即可. #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespa

2016&quot;百度之星&quot; - 初赛(Astar Round2A)Gym Class(拓扑排序)

Gym Class Accepts: 849 Submissions: 4247 Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到NN,在排好队之后,每个同学会找

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

Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 768    Accepted Submission(s): 309 Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有

6081: Gym Class(拓扑排序+优先队列)

6081: Gym Class 时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte 总提交: 40            测试通过:10 描述 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到N,在排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.麻烦的是,有一些同学不希望某个(

Gym 100512G Grand Tour (拓扑排序)

题意:一个团队要去参观一些学校,某些学校要在某些学校之前先参观,并且每个学校有一个权值,团队去的时间与权值的差作为难过度(最小是0), 所有的难过度的最大值是伤心度,让你安排参观顺序,使得这个伤心度最小. 析:拓扑排序,并且要逆序排,这样的话,时间大的优先,可以用优先队列实现. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string

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

分析:首先,利用贪心可知,如果要所有人的分数和最高,需要把序号大的优先放在前面.其次,对于a的前面不能为b,那么只能a在b前面了,那么就建立一条从a到b的边,并且b的入度加1.然后就是拓扑排序了.要分数最高,则把哪些入度为0的点(他们不需要有哪些人一定要在他们前面,最自由)丢进优先队列,然后就可以实现把序号大的尽量放在前面而且满足题意了. 具体见代码: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string

拓扑排序讲解

在这里我们要说的拓扑排序是有前提的 我们在这里说的拓扑排序是基于有向无环图的!!!. (⊙o⊙)…我所说的有向无环图都知道是什么东西吧.. 如果不知道,我们下面先来来说说什么是有向无环图. 所谓有向无环图,顾名思义是不存在环的有向图(至于有向图是什么不知道的在前面我们有一个图论讲解上都有). 点的入度:以这个点为结束点的边数. 点的出度:以这个点为出发点的边的条数. 拓扑序就是对于一个节点的一个排列,使得(u,v)属于E,那么u一定出现在v的前面.然而拓扑排序就是一个用来求拓扑序的东西. 对于左

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>