首页 > 编程语言 > 详细

Gym 100792 King's Rout 拓扑排序

时间:2018-04-09 22:37:26      阅读:310      评论:0      收藏:0      [点我收藏+]
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 拓扑排序

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!