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. unittest核心要素

    1 TestCase 一个TestCase的实例就是一个测试用例.什么是测试用例呢?就是一个完整的测试流程, 包括测试环境的准备(setUp),执行测试代码(run),以及测试后环境的还原(tearD ...

  2. html的表格边框为什么会这么粗?

    因为默认情况下,cellspacing = 2px. 当表格的 border 不为 0 的时候,单元格(cell)的 border 为 1. 只有当表格的 border 设置为 0 的时候,单元格的 ...

  3. SSM整合activiti框架

    一:WorkFlow简介 1:什么是工作流工作流(Workflow),指“业务过程的部分或整体在计算机应用环境下的自动化”.是对工作流程及其各操作步骤之间业务规则的抽象.概括描述.在计算机中,工作流属 ...

  4. python+selenium六:隐式等待

    python+selenium六:隐式等待   # 隐式等待 # 全局生效,只写一次即可(仅当前页面)# 若有页面切换,需sleep等待新页面出现后,再使用此方法 # 如:在35秒内,等待操作完成,完 ...

  5. Nginx 的三大功能

    1.HTTP服务器 Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 2.反向代理服务器 Nginx也是反向代理服务器. 说反向代理之前先说一 ...

  6. FourAndSix2 靶机渗透

    0x01 简介 FourAndSix2是易受攻击的一个靶机,主要任务是通过入侵进入到目标靶机系统然后提权,并在root目录中并读取flag.tx信息 FourAndSix2.镜像下载地址: https ...

  7. pycharm最新版本激活码(永久有效) python安装教程

    Mac 系统自带python 1.打开终端, 输入 python 可以查看python当前版本. 2.输入“python”回车后即进入解释器,例如打印“hello world!”, 可输入 ‘ pri ...

  8. React学习笔记(持续更新)

    2.2页面加载过程 1.资源加载过程:URL->DNS查询->资源请求->浏览器解析 ①URL结构:http://www.hhh.com:80/getdata?pid=1#title ...

  9. 夯实Java基础系列4:一文了解final关键字的特性、使用方法,以及实现原理

    目录 final使用 final变量 final修饰基本数据类型变量和引用 final类 final关键字的知识点 final关键字的最佳实践 final的用法 关于空白final final内存分配 ...

  10. 后端(spring boot)解决跨区域问题

    一.环境: 前端 vue element-ui 后端:spring boot 工具:IDEA Maven Node 数据库:MySql 二.首先我们需要了解什么叫跨区域访问问题 跨区域访问是指:不同域 ...