Codeforces Gym 100792K King's Rout

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 0000 ≤ 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.


题目大意:
有编号为1~n的客人要去吃饭,然后他们会先后到达。 
现在已经知道了有些人一定会先于另外有些人到达,
有m个先后关系,要求a比b要先到, 同时要尽量使编号小的人先到达。

存图时是b->a单向
然后呢 在拓扑排序时
在入度为0的点中, 因为入度为0,所以就不需要管先后顺序, 直接让他们最后到达 
而在这些入度为0的点中 因为要使编号小的人先到达 因此就先push编号大的(ans是倒过来输出的) 
用优先队列实现 
想一想 画一画 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define debug(a) printf("---%d---\n", a)
#define mem0(a) memset(a, 0, sizeof(a))
#define memi(a) memset(a, inf, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 2e6 + 5;
const int mod = 1e8;

vector<int>mp[maxn];
vector<int>ans;
int ind[maxn], n, m; 
void topsort()
{
//	priority_queue<int, vector<int>, less<int> >q;
	priority_queue<int> q;
	for (int i = 1; i <= n; i++)
		if (ind[i] == 0) q.push(i);
	while (!q.empty())
	{
		int u = q.top(); q.pop();
		ans.push_back(u);
		for (int i = 0; i < mp[u].size(); i++){
			int v = mp[u][i];
			ind[v]--;
			if (ind[v] == 0) q.push(v);
		}
	}
}
int main(void)
{
	int a, b;
	while (cin >> n >> m)
	{
		memset(mp, 0, sizeof(mp));
		memset(ind, 0, sizeof(ind));
		ans.clear();
		for (int i = 1; i <= m; i++){
			scanf("%d %d", &a, &b);
			mp[b].push_back(a);
			ind[a]++;
		}
		topsort();
		for (int i = ans.size()-1; i >= 0; i--)
			printf("%d%c", ans[i], i==0?'\n':' ');
	}
	
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值