Background\text{Background}Background

I’ve got that Luogu Dialy has been \text{I've got that Luogu Dialy has been }I’ve got that Luogu Dialy has been updated\text{updated}updated this morning.\text{ this morning.} this morning.

So I decided to learn Topological Sort.\text{So I decided to learn Topological Sort.}So I decided to learn Topological Sort.

该死的蚊子,TMD把我的手咬得像耕地一样,昨晚皮肤过敏挣扎到很晚才睡。

Description\text{Description}Description

You’ve got N dishes and M rules about their orders.\text{You've got }N\text{ dishes and }M\text{ rules about their orders.}You’ve got N dishes and M rules about their orders.

Each rule seems like &lt;i,j&gt;, it means you must do i first before doing j.\text{Each rule seems like }&lt;i,j&gt;,\text{ it means you must do }i\text{ first before doing }j.Each rule seems like <i,j>, it means you must do i first before doing j.

You perfer dish 1 first, then dish 2,3, and so on.\text{You perfer dish }1\text{ first, then dish 2,3, and so on.}You perfer dish 1 first, then dish 2,3, and so on.

Please find an order to do all these dishes, so that you can have dish 1 as quickly as possible.\text{Please find an order to do all these dishes, so that you can have dish 1 as quickly as possible.}Please find an order to do all these dishes, so that you can have dish 1 as quickly as possible.

If 2 plans’ 1’s order is the same, than you perfer which can have 2 as quickly as\text{If 2 plans' 1's order is the same, than you perfer which can have 2 as quickly as}If 2 plans’ 1’s order is the same, than you perfer which can have 2 as quickly as

possble (And so on).\text{possble (And so on).}possble (And so on).

Solution\text{Solution}Solution

There’s a tips that if you output the lexicographic-ordered plan, you’ll get a WA.\text{There's a tips that if you output the lexicographic-ordered plan, you'll get a WA.}There’s a tips that if you output the lexicographic-ordered plan, you’ll get a WA.

The correct solution is to create the inverse graph, and output upside down.\text{The correct solution is to create the inverse graph, and output upside down.}The correct solution is to create the inverse graph, and output upside down.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue> using namespace std; #define reg register struct node{
int x,y,next;
}e[100010];
int len=0;
int T;
int n,m,cnt;
int sx,sy;
int first[100010];
int out[100010];
priority_queue<int> q;
int ans[100010]; void ins(int x,int y){
e[++len].x=x;e[len].y=y;++out[y];
e[len].next=first[x];first[x]=len;
}
void Toposort(){
while(!q.empty()) q.pop();
for(reg int i=1;i<=n;++i)
if(!out[i])
q.push(i);
while(!q.empty()){
int x=q.top();q.pop();
ans[cnt--]=x;
for(reg int i=first[x];i;i=e[i].next){
int y=e[i].y;--out[y];
if(!out[y])
q.push(y);
}
}
}
int main(){
scanf("%d",&T);
while(T--){
memset(first,0,sizeof(first));len=0;
memset(out,0,sizeof(out));
scanf("%d%d",&n,&m);
for(reg int i=1;i<=m;++i){
scanf("%d%d",&sx,&sy);
ins(sy,sx);
}
cnt=n;
Toposort();
if(!cnt){
for(reg int i=1;i<=n;++i)
printf("%d ",ans[i]);
puts("");
}
else
printf("Impossible!\n");
}
}

Making Dishes (P3243 [HNOI2015]菜肴制作)的更多相关文章

  1. P3243 [HNOI2015]菜肴制作(拓扑排序)

    P3243 [HNOI2015]菜肴制作 题目误导你正着做拓扑排序,然鹅你可以手造数据推翻它.于是就只能倒着做 我们开个优先队列,每次把可填的最大的编号取出来搞,最后倒着输出拓扑序就好辣 #inclu ...

  2. P3243 [HNOI2015]菜肴制作

    传送门 把时间看成数,菜肴看成位置 考虑一个位置填什么数很麻烦 考虑一个数放在什么位置 一开始我想的是,对于一个限制 $(a,b)$ ,从 $a$ 往 $b$ 连一条边,然后如果有解则所有的限制构成了 ...

  3. 洛谷P3243 [HNOI2015]菜肴制作——拓扑排序

    题目:https://www.luogu.org/problemnew/show/P3243 正向按字典序拓扑排序很容易发现是不对的,因为并不是序号小的一定先做: 但若让序号大的尽可能放在后面,则不会 ...

  4. luogu P3243 [HNOI2015]菜肴制作

    这题一看就知道和拓扑序有关 考虑拓扑排序的时候每次取队列里最小的数进行排序 然后就\(\mathcal{GG}\)了,因为这样只能使字典序最小,而并不能保证题目中要求的每个编号的数要在满足前面数尽量在 ...

  5. 洛谷P3243 [HNOI2015]菜肴制作 拓扑排序+贪心

    正解:拓扑排序 解题报告: 传送门! 首先看到它这个约束就应该要想到拓扑排序辣QwQ 首先想到的应该是用优先队列代替队列,按照节点编号排序 然后也很容易被hack:<5,1> 正解应为5, ...

  6. 洛谷 P3243 [HNOI2015]菜肴制作 题解

    每日一题 day60 打卡 Analysis 这道题一看就感觉是个拓扑排序,但因为按字典序最小的排序会有问题(见第三个样例)主要原因是每次选择有后效性,而从后往前就不会存在这个问题,因为每个子任务都是 ...

  7. 洛谷P3243 [HNOI2015]菜肴制作 (拓扑排序/贪心)

    这道题的贪心思路可真是很难证明啊...... 对于<i,j>的限制(i必须在j之前),容易想到topsort,每次在入度为0的点中选取最小的.但这种正向找是错误的,题目要求的是小的节点尽量 ...

  8. bzoj 4010: [HNOI2015]菜肴制作 拓扑排序

    题目链接: 题目 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB 问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴 ...

  9. BZOJ 4010: [HNOI2015]菜肴制作( 贪心 )

    把图反向,然后按拓扑序贪心地从大到小选, 最后输出.set比priority_queue慢... --------------------------------------------------- ...

随机推荐

  1. ThreadLocal的认知与见解

    ThreadLocal:提高一个线程的局部变量,访问某个线程拥有自己局部变量(很难理解.看看下面这句话,顺便再讲个例子). 当使用ThreadLocal维护变量时,ThreadLocal为每一个使用该 ...

  2. charles DNS欺骗

    本文参考:charles DNS欺骗 DNS欺骗/DNS Spoofing 功能:通过将您自己的主机名指定给远程地址映射来欺骗DNS查找 一般的开发流程中,在上线之前都需要在测试环境中先行进行验证,而 ...

  3. Spring boot 自定义banner的在线制作

    目前工作不是很忙,利用闲暇的时间,在给自己不断地充电,提升自己的技术实力. 目前在做一个基于Spring Boot2.x+webmagic+quartz的爬虫项目[hotDog]https://git ...

  4. [VB.NET Tips]ParamArray参数数组

    ParamArray参数数组,可以理解为传递给方法的多余的参数全都存放在这个数组中. ParamArray只能是ByVal按值传递,不能是可选参数,而且只能做为方法定义的最后一个参数. 非常类似于Py ...

  5. 利用IntelliJ IDEA与Maven开发scala程序,并打包提交到spark集群

    https://zhuanlan.zhihu.com/p/23141509 https://blog.csdn.net/u011470552/article/details/54564636 http ...

  6. 用 C# 来守护 Python 进程

    背景 目前我主要负责的一个项目是一个 C/S 架构的客户端开发,前端主要是通过 WPF 相关技术来实现,后端是通过 Python 来实现,前后端的数据通信则是通过 MQ 的方式来进行处理.由于 Pyt ...

  7. mysql修改数据库的存储引擎(InnoDB)

    查看当前的存储引擎 show engines; 基本的差别:MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持.MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不 ...

  8. 【Django】ajax(多对多表单)

    1.前后端交互 <div class="shade hide"></div> <!--遮罩层,全屏--> <div class=" ...

  9. Spring MVC-从零开始-如何访问静态资源

    转(Spring MVC静态资源处理)   优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置Dis ...

  10. 【IT技术概念】什么是webservice?

    WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互. WebServi ...