题意

题目链接

Sol

非常妙的一道题。

首先不难想到拓扑排序,但是直接对原图按\(k\)从小到大拓扑排序是错的。因为当前的\(k\)大并不意味着后面的点\(k\)也大

但是在反图上按\(k\)从大到小拓扑排序就是对的。为什么呢?因为题目中给出的条件是下限, 而在反图上拓扑排序就相当于卡着下限做,因此一定是最优的

对于第二问,同样在反图上搞。对每个点分开做,贪心的策略是:如果有其他的飞机可以起飞则让他们起飞,直到没有飞机可以起飞,这时的时间就是答案

// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
const int MAXN = 2e5 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, a[MAXN], inder[MAXN], tmp[MAXN], ans[MAXN];
vector<int> v[MAXN];
void Topsort() {
priority_queue<Pair> q;
for(int i = 1; i <= N; i++)
if(!inder[i]) q.push(MP(a[i], i));
int tot = 0;
while(!q.empty()) {
int p = q.top().se; q.pop(); ans[++tot] = p;
for(int i = 0, to; i < v[p].size(); i++) {
to = v[p][i];
inder[to]--;
if(!inder[to]) q.push(MP(a[to], to));
}
}
for(int i = tot; i >= 1; i--) printf("%d ", ans[i]); puts("");
}
int solve(int x) {
memcpy(inder, tmp, sizeof(tmp));
priority_queue<Pair> q;
inder[x] = N;
for(int i = 1; i <= N; i++) if(!inder[i]) q.push(MP(a[i], i));
int tim = N;
for(int i = N; i; i--) {
if(q.empty() || (q.top().fi < i)) return i;
int p = q.top().se; q.pop();
for(int i = 0, to; i < v[p].size(); i++) {
to = v[p][i];
inder[to]--;
if(!inder[to]) q.push(MP(a[to], to));
}
}
return tim;
}
int main() {
N = read(); M = read();
for(int i = 1; i <= N; i++) a[i] = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read();
v[y].push_back(x); inder[x]++; tmp[x]++;
}
Topsort();
for(int i = 1; i <= N; i++) printf("%d ", solve(i));
return 0;
}
/*
10 10
4 4 3 6 9 9 10 7 10 7
2 9
3 5
6 7
1 5
7 9
10 2
3 8
8 6
3 10
8 5 */

最新文章

  1. Jenkins pipeline 入门到精通系列文章
  2. CMD和AMD
  3. [LeetCode]题解(python):056-Merge Intervals
  4. JavaScript访问修改css样式表
  5. windows上安装winsshd
  6. 应用facebook .net sdk
  7. Python+Requests接口测试教程(1):Fiddler抓包工具
  8. VB.NET 打开窗体后关闭自己
  9. 搭建Jetbrains家族IDE授权服务器
  10. WPF线程中获取控件的值和给控件赋值
  11. 将Paul替换成Ringo
  12. 软件工程第4次作业------石墨文档Android客户端案例分析
  13. [java]给出一个字符串,将重复的字符去除,仅保留第一次出现的字符,且保持去重后的字符在原字符串中的顺序不变
  14. 关于html5中的 网页图标问题
  15. solr联合多个字段进行检索(multivalued和copyfield的使用)
  16. NGUI可展开列表的实现
  17. 扫盲:java中关于路径的问题
  18. poj3814
  19. iOS:分组的表格视图UITableView,可以折叠和展开
  20. thinkjs 框架图

热门文章

  1. 最小生成树问题:Kruskal算法 AND Prim算法
  2. 条目四《用empty来代替检查size()是否为0》
  3. 微信小程序获取用户手机号,服务器解码demo
  4. MacOs桌面自动被打乱的原因
  5. tornado 06 数据库—ORM—SQLAlchemy——基本内容及操作
  6. C++_类入门4-String类
  7. 多气体组分DEM流动的DMP并行内存错误
  8. WPF 一 概述
  9. BZOJ - 4260 01字典树+前后缀
  10. PIE.NET-SDK组件式二次开发文档