ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout
4 seconds
512 megabytes
standard input
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.
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 ai, bi (1 ≤ ai, bi ≤ n). That means the guest ai is required to come earlier than the guest bi.
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.
3 1
3 1
3 1 2
5 6
2 1
5 2
4 1
5 4
3 1
5 3
5 2 3 4 1
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条有向边,求字典序最小的topo排序。
分析:注意是字典序最小。
有优先队列小心处理下即可。
小心被坑
/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , M = ;
int n, m;
int cnt[N];
vector<int> fa[N];
priority_queue<int> ranks;
vector<int> ans; inline void Input()
{
scanf("%d%d", &n, &m);
for(int i = ; i < m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
fa[y].pub(x), cnt[x]++;
}
} inline void Solve()
{
for(int i = ; i <= n; i++)
if(!cnt[i]) ranks.push(i);
for(int index = n; index >= ; index--)
{
int u = ranks.top();
ranks.pop();
ans.pub(u);
int length = sz(fa[u]);
for(int i = , v; i < length; i++)
if(!(--cnt[v = fa[u][i]]))
ranks.push(v);
} int length = sz(ans);
for(int i = length - ; i > ; i--)
printf("%d ", ans[i]);
printf("%d\n", ans[]);
} int main()
{
Input();
Solve();
return ;
}
ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout的更多相关文章
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering
Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time
Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?
I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing
H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 C. Colder-Hotter
C. Colder-Hotter time limit per test 1 second memory limit per test 512 megabytes input standard inp ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams
A. Anagrams time limit per test 1 second memory limit per test 512 megabytes input standard input ou ...
- hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online
Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...
- 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)
队名:Unlimited Code Works(无尽编码) 队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...
- Moscow Subregional 2013. 部分题题解 (6/12)
Moscow Subregional 2013. 比赛连接 http://opentrains.snarknews.info/~ejudge/team.cgi?contest_id=006570 总叙 ...
随机推荐
- 四、优化及调试--网站优化--SEO在网页制作中的应用
SEO分类:白帽SEO.黑帽SEO 白帽SEO: 内容上的SEO: 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 前端SEO: 网站结构布局优化 扁平化结构(一 ...
- mysql里表以及列的增删改查
在一个表里插入数据(增) insert into 表名 (需要插入的列名如 id,name,age)values (1,'张三',20), (2,'李四',30): 查询表内容(查 ...
- mysql入门语句10条
1,连接数据库服务器 mysql -h host -u root -p xxx(密码) 2,查看所有库 show databases; 3,选库 use 库名 4,查看库下面的表 show ...
- ListView + PopupWindow实现滑动删除
原文:ListView滑动删除 ,仿腾讯QQ(鸿洋_) 文章实现的功能是:在ListView的Item上从右向左滑时,出现删除按钮,点击删除按钮把Item删除. 看过文章后,感觉没有必要把dispat ...
- mysql的存储过程
mysql5中开始引入存储过程,存储过程是一组为了完成特定功能的sql语句集,经编译后存储在数据库中. 存储过程的特点(优点): 1:减小网络通信量.调用一个行数不多的存储过程与直接高用SQL语名的网 ...
- 同一内网不能网段ping 不通
[root@NB sysconfig]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use ...
- bootstrap的验证和确认对话框
BootstrapValidator: http://bv.doc.javake.cn/api/ 引用 <!-- jquery-confirm.确认对话框 --> <link ...
- Ubuntu开机自启动
http://www.jb51.net/os/Ubuntu/181138.html http://blog.csdn.net/elim051/article/details/6173367
- java中常用的工具类(二)
下面继续分享java中常用的一些工具类,希望给大家带来帮助! 1.FtpUtil Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- 安装oracle 10g RAC执行的几个脚本说明
1,/u01/app/oracle/oraInventory/orainstRoot.sh 脚本 #!/bin/sh if [ -d "/etc" ]; then /etc; fi ...