Gym 100792 King's Rout 拓扑排序

Posted 灬从此以后灬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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+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 拓扑排序的主要内容,如果未能解决你的问题,请参考以下文章

Gym 101933 King's Colors

CF gym 101933 K King's Colors —— 二项式反演

CF gym 101933 K. King's Colors(二项式反演)

Gym 100792C Colder-Hotter (三分)

Gym 101666K King of the Waves(dfs)

King Gym - 102471H