BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心
BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心
Description
知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴。
Input
第一行是一个正整数D,表示数据组数。
Output
输出文件仅包含 D 行,每行 N 个整数,表示最优的菜肴制作顺序,或
Sample Input
5 4
5 4
5 3
4 2
3 2
3 3
1 2
2 3
3 1
5 2
5 2
4 3
Sample Output
Impossible!
1 5 2 4 3
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_pbds;
__gnu_pbds::priority_queue<int>q;
#define N 100050
int head[N],to[N],nxt[N],indu[N],cnt,n,ans[N],m;
inline void add(int u,int v) {
to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
int i,x,y;
memset(head,0,sizeof(head)); cnt=0;
memset(indu,0,sizeof(indu));
for(i=1;i<=m;i++) {
scanf("%d%d",&x,&y); add(y,x); indu[x]++;
}
int cnt=0;
for(i=1;i<=n;i++) if(!indu[i]) q.push(i);
while(!q.empty()) {
int x=q.top(); q.pop();
ans[++cnt]=x;
for(i=head[x];i;i=nxt[i]) {
indu[to[i]]--;
if(!indu[to[i]]) q.push(to[i]);
}
}
if(cnt!=n) puts("Impossible!");
else {
for(i=cnt;i;i--) printf("%d ",ans[i]); puts("");
}
}
}
BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心的更多相关文章
- BZOJ4010 [HNOI2015]菜肴制作 【拓扑排序 + 贪心】
题目 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予 1到N的顺序编号,预估质量最高的菜肴编号为1.由于菜肴之间 ...
- bzoj4010: [HNOI2015]菜肴制作(拓扑排序+贪心+堆)
这题不是求最小字典序...撕烤了半个小时才发现不对劲T T 这题是能让小的尽量前就尽量前,无论字典序...比如1能在2前面就一定要在2前面... 显然是要先拓扑排序,让小的尽量前转化成让大的尽量往后丢 ...
- 洛谷P3243 [HNOI2015]菜肴制作 (拓扑排序/贪心)
这道题的贪心思路可真是很难证明啊...... 对于<i,j>的限制(i必须在j之前),容易想到topsort,每次在入度为0的点中选取最小的.但这种正向找是错误的,题目要求的是小的节点尽量 ...
- P3243 [HNOI2015]菜肴制作(拓扑排序)
P3243 [HNOI2015]菜肴制作 题目误导你正着做拓扑排序,然鹅你可以手造数据推翻它.于是就只能倒着做 我们开个优先队列,每次把可填的最大的编号取出来搞,最后倒着输出拓扑序就好辣 #inclu ...
- [HNOI2015]菜肴制作(拓扑排序)
知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1. 由于菜肴之间口味搭 ...
- 【题解】 [HNOI2015]菜肴制作 (拓扑排序)
题目描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1. 由于菜肴 ...
- [BZOJ4010]:[HNOI2015]菜肴制作(拓扑排序)
题目传送门 题目描述 知名美食家小A被邀请至ATM大酒店,为其品评菜肴. ATM酒店为小A准备了N道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1.由于菜肴之间 ...
- bzoj4010: [HNOI2015]菜肴制作【拓扑排序】
想到了一个分治方法,每一次尽量放小的那个,把它依赖的放在左边,不依赖的放在右边. TLE 80: #include <bits/stdc++.h> #define rep(i, a, b) ...
- bzoj 4010: [HNOI2015]菜肴制作【拓扑排序】
也就是给定有向图,求最小字典序的拓扑序,直接用小根堆就行(或者反着建图用大根堆) #include<iostream> #include<cstdio> #include< ...
随机推荐
- Unable to connect to the MKS : Failed to connect to server XXXXXX:903
Issue : 通过 vSphere 连接VM控制台的时候, 报错 Unable to connect to the MKS : Failed to connect to server X ...
- Celery 启动报错 can_read() got an unexpected keyword argument timeout
问题: Celery 启动报错 can_read() got an unexpected keyword argument timeout 方案:更改redis版本和celery版本,我使用下面的ce ...
- 【翻译自mos文章】当并行事务恢复进程在执行时,禁用并行事务恢复的方法
当并行事务恢复进程在执行时,禁用并行事务恢复的方法 How to Disable Parallel Transaction Recovery When Parallel Txn Recovery is ...
- [Algorithms] Tree Data Structure in JavaScript
In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...
- hdu1827之强联通
Summer Holiday Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 使用cacheBuilder实现函数防抖
在接口中出现的相同请求重复且连续发送的情况导致一些业务BUG,需要在接口上实现防抖 使用google的cacheBuilder import com.google.common.cache.Cache ...
- python3 查看已安装的模块
一.命令行下使用pydoc命令 在命令行下运行$ pydoc modules即可查看 二.在python交互解释器中使用help()查看 在交互式解释器中输入>>> help(&qu ...
- 在jsp中拿到applicationContext
WebApplicationContext wac = (WebApplicationContext)config.getServletContext().getAttribute(WebApplic ...
- vue2.0 + vux (四)Home页
1.综合页(首页) Home.vue <!-- 首页 --> <template> <div> <!-- 顶部 标题栏 --> <app-head ...
- mysql 加密解密函数
http://blog.csdn.net/wh62592855/article/details/6777753 mysql下的加密函数有如下几个 PASSWORD():创建一个经过加密的密码字符串,适 ...