【HNOI2015】菜肴制作
题面
题解
这道题目首先可以想到拓扑排序,但是肯定不是字典序最小的排列。
比如说,有\(4\)种菜,限制为\(2 \to 4, 3 \to 1\),那么如果求字典序最小的排列会算出\((2, 3, 1, 4)\),但是答案显然是\((3, 1, 2, 4)\)。
于是,正难则反,发现如果最后一个数字在合法的范围内尽可能的大,那么就会更优。
我们就可以求字典序反序最大的排列。
于是建反图,跑拓扑排序即可,这里的拓扑排序要用到堆。
代码
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<queue>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int maxn(3e5 + 10);
std::priority_queue<int> heap;
struct edge { int next, to; } e[maxn];
int head[maxn], e_num, n, m, deg[maxn], T, cnt, ans[maxn];
inline void add_edge(int from, int to)
{
e[++e_num] = (edge) {head[from], to};
head[from] = e_num; ++deg[to];
}
int main()
{
T = read();
while(T--)
{
clear(head, 0); e_num = 0;
clear(deg, 0);
n = read(); m = read();
int f = 0; cnt = 0;
for(RG int i = 1, x, y; i <= m; i++)
{
x = read(), y = read(), add_edge(y, x);
if(x == y) f = 1;
}
if(f) { puts("Impossible!"); continue; }
for(RG int i = 1; i <= n; i++) if(!deg[i]) heap.push(i);
while(!heap.empty())
{
int x = heap.top(); heap.pop(); ans[++cnt] = x;
for(RG int i = head[x]; i; i = e[i].next)
{
int to = e[i].to;
if(!(--deg[to])) heap.push(to);
}
}
if(cnt < n) puts("Impossible!");
else { for(RG int i = n; i; i--) printf("%d ", ans[i]); puts(""); }
}
return 0;
}
【HNOI2015】菜肴制作的更多相关文章
- bzoj 4010: [HNOI2015]菜肴制作 拓扑排序
题目链接: 题目 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB 问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴 ...
- BZOJ 4010: [HNOI2015]菜肴制作( 贪心 )
把图反向,然后按拓扑序贪心地从大到小选, 最后输出.set比priority_queue慢... --------------------------------------------------- ...
- P3243 [HNOI2015]菜肴制作(拓扑排序)
P3243 [HNOI2015]菜肴制作 题目误导你正着做拓扑排序,然鹅你可以手造数据推翻它.于是就只能倒着做 我们开个优先队列,每次把可填的最大的编号取出来搞,最后倒着输出拓扑序就好辣 #inclu ...
- bzoj 4010 [HNOI2015]菜肴制作——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4010 和 bzoj 2535 差不多.因为当前怎么决策与该点后面连的点的标号情况有关,所以按 ...
- 【BZOJ4010】[HNOI2015]菜肴制作 拓扑排序
[BZOJ4010][HNOI2015]菜肴制作 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高 ...
- BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心
BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...
- [HNOI2015]菜肴制作 题解(自带口胡证明)
[HNOI2015]菜肴制作 时间限制: 1 Sec 内存限制: 512 MB 题目描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为 ...
- [HNOI2015]菜肴制作贪心的证明
[HNOI2015]菜肴制作贪心的证明 先吐槽一句为什么网上都没人证这个东西,我觉得一点也不显然啊... 判环不用说了,现在处理一个DAG.考虑按题意模拟:建反图(边从后选的点连向先选的点),每次找全 ...
- 【题解】[HNOI2015]菜肴制作(贪心+topo序)
[题解][HNOI2015]菜肴制作(贪心+topo序) 题意:请你构造一个排列\(p[i]\)使得对于数组\(arc[i]=p[i]\)的字典序最小,并且对于给定的有序数对\((u,v)\)保证你给 ...
- [bzoj4010][HNOI2015]菜肴制作_贪心_拓扑排序
菜肴制作 bzoj-4010 HNOI-2015 题目大意:给定一张n个点m条边的有向图,求一个toposort,使得:(1)满足编号为1的点尽量在前:(2)满足(1)的情况下编号为2的点尽量在前,以 ...
随机推荐
- 如何判断一个整数是否是2的N次幂
static bool CheckPowerOfTwo(ulong num) { && (num & (num - )) == ; }
- js过滤HTML标签以及
function removeHTMLTag(str) { str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag str = str.r ...
- UIButton的两种block传值方式
UIButton的两种block传值方式 方式1 - 作为属性来传值 BlockView.h 与 BlockView.m // // BlockView.h // Block // // Create ...
- UIButton的resizableImageWithCapInsets使用解析
UIButton的resizableImageWithCapInsets使用解析 效果: 使用的源文件: 源码: // // ViewController.m // SpecialButton // ...
- procexp
https://www.cnblogs.com/iTBear/articles/2789151.html
- Ubuntu16.04安装redis和php的redis扩展
安装redis服务 sudo apt-get install redis-server 装好之后默认就是自启动.后台运行的,无需过多设置,安装目录应该是 /etc/redis 启动 sudo ser ...
- Ubuntu 14.04安装QQ2012
GTkqq ,pidginQQ........等多多少少都存在一定的缺陷和问题. linuxQQ 有各种版本,这里介绍两种:linuxQQ(基本已不支持) 和 wineQQ (推荐使用) 1 ---- ...
- 4-6 R语言函数 排序
#sort:对向量进行排序;返回排好序的内容 #order:返回排好序的内容的下标/多个排序标准 > x <- data.frame(v1=1:5,v2=c(10,7,9,6,8),v3= ...
- HDU4513:完美队形II(Manacher)
Description Input Output Sample Input Sample Output Solution 才发现我之前不会证$Manacher$复杂度……QAQ 题意是求最长向 ...
- weblogic之CVE-2018-3191漏洞分析
weblogic之CVE-2018-3191漏洞分析 理解这个漏洞首先需要看这篇文章:https://www.cnblogs.com/afanti/p/10193169.html 引用廖新喜说的,说白 ...