2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest K

330人阅读 评论(0) 收藏 举报

K. King’s Rout
time limit per test4 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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.


为什么对呢,这样看:反过来排,后k个数已确定,这时候第k-1个数大的方案,显然好于第k-1个数小的方案。例如1 2 3 4排,最后一个填了3,而第3个空可以填2或4,这样必然填4。若填了2,结果无非是4 1 2 3或1 4 2 3,而对应地将2和4交换后会得到一个更优的序列2 1 4 3或1 2 4 3(交换的正确性仔细想想便知),然后就是一个类似贪心的过程。


// ml:run = $bin < input
#include <bits/stdc++.h>
using namespace std;

int n, m;
int indegree[205000];
bool vis[205000];
int ans[205000];

void toposort()
    std::priority_queue<int> q;
    for (int i = 1; i <= n; i++)
        if (!indegree[i])

    for (int ti = 1; !q.empty(); ti++) {
        int now = q.top(); q.pop();
        ans[ti] = now;
        for (int j = 0; j < (int)g[now].size(); j++) {
            int v = g[now][j];
            if (!indegree[v]) q.push(v);

int main()
    scanf("%d%d", &n, &m);
    memset(vis, false, sizeof(vis));
    memset(indegree, 0, sizeof(indegree));
    for(int i=1;i<=n;i++)g[i].clear();
    int x, y;
    for(int i=1;i<=m;i++)
        scanf("%d%d", &x, &y);
    for(int i=n;i>=1;i--)
        printf("%d%c", ans[i], i==1?'\n':' ');
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest G】【数据结构-线段树】Hiring 准备时间完成时间最早完成日期

G. Hiring time limit per test4 seconds memory limit per test512 megabytes inputstandard input output...
  • snowy_smile
  • snowy_smile
  • 2015-11-08 13:38
  • 566

【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest B】【暴力双排序】Layer Cake 若干矩形 选择相同长和宽的最大体积

B. Layer Cake time limit per test6 seconds memory limit per test512 megabytes inputstandard input ou...
  • snowy_smile
  • snowy_smile
  • 2015-11-08 12:44
  • 451

【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest E】【模拟 贪心】Easy Arithmetic 需要补 加减法表达式添加加减号使得数

#include #include #include #include #include #include #include #include #include #include #include #...
  • snowy_smile
  • snowy_smile
  • 2015-11-15 20:47
  • 659

【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest H】【字符串哈希 构造】Hash Code Hacker 构造有相同哈希值的不同字符串

#include #include #include #include #include #include #include #include #include #include #include ...
  • snowy_smile
  • snowy_smile
  • 2015-11-15 21:33
  • 777

【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest G】【脑洞 拓扑排序 大根堆小根堆极限维护】Graph DAG加k边使得最小拓扑序最大

#include #include #include #include #include #include #include #include #include #include #include ...
  • snowy_smile
  • snowy_smile
  • 2015-11-15 20:48
  • 774

【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest C】【脑洞 本质不同实现的转化】Concatenation A串为前缀B串为后缀不同串

#include #include #define MS(x,y) memset(x,y,sizeof(x)) typedef long long LL; const int N=1e5+10,M=...
  • snowy_smile
  • snowy_smile
  • 2015-11-15 20:44
  • 690

第41届ACM国际大学生程序设计竞赛亚洲区域赛(青岛) 参赛总结

  • Kiritow
  • Kiritow
  • 2016-11-14 08:02
  • 1118


又在挖坑了 A #include #include #include #include #include #include #include #include #include ...
  • sasuke__
  • sasuke__
  • 2017-12-20 12:26
  • 58

2017 ACM-ICPC 沈阳站总结!

一次惊险的比赛体验  今年开黑打网络赛侥幸得到一个名额, 于是和软件商量,软件去哈尔滨,我们队去沈阳。 火车25个小时20号周五下午三点左右到达沈阳,挤着2号线到东大附近,然后吹风问路,到宾馆,晚...
  • 2017-10-24 11:13
  • 791


时间如白驹过隙,转眼间就到了2017年、大三上半学期。这样算来搞ACM也 有一年多的光景了。之前因为一直在准备比赛,没有写比赛经历总结,最近 刚刚打完Final,算是准退役了把。想想也是时候来篇Sum...
  • chudongfang2015
  • chudongfang2015
  • 2017-12-22 09:02
  • 204
    • 访问:21188次
    • 积分:1071
    • 等级:
    • 排名:千里之外
    • 原创:89篇
    • 转载:1篇
    • 译文:0篇
    • 评论:5条
    http://izihan.cf/ http://blog.csdn.net/Danliwoo