BZOJ2535 [Noi2010]Plane 航空管制 【贪心 + 堆】
题目链接
题解
航班之间的关系形成了一个拓扑图
而且航班若要合法,应尽量早出发
所以我们逆拓扑序选点,能在后面出发的尽量后面出发,不会使其它点变得更劣,容易知是正确的
第二问只需枚举航班\(x\),拓扑排序时忽视\(x\),最后无法选点时就是\(x\)最早的时间
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (register int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (register int i = 1; i <= (n); i++)
#define res register
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 2005,maxm = 20005,INF = 1000000000;
inline int read(){
res int out = 0,flag = 1; res char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,inde[maxn],K[maxn],val[maxn],ans[maxn];
int h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
inline void build(int u,int v){
ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;
inde[v]++;
}
priority_queue<cp> q;
inline void solve1(){
REP(i,n){
val[i] = inde[i];
if (!val[i]) q.push(mp(K[i],i));
}
cp u;
for (int i = n; i; i--){
u = q.top(); q.pop();
ans[i] = u.second;
Redge(u.second){
if (!(--val[to = ed[k].to])) q.push(mp(K[to],to));
}
}
REP(i,n){
printf("%d",ans[i]);
if (i < n) putchar(' ');
}puts("");
}
inline int solve2(int x){
while (!q.empty()) q.pop();
REP(j,n) val[j] = inde[j]; val[x] = n;
REP(j,n) if (!val[j]) q.push(mp(K[j],j));
cp u;
for (res int j = n; j; j--){
if (q.empty()) return j;
u = q.top(); q.pop();
if (u.first < j) return j;
Redge(u.second) if (!(--val[to = ed[k].to])) q.push(mp(K[to],to));
}
return 1;
}
int main(){
n = read(); m = read();
REP(i,n) K[i] = read();
int a,b;
REP(i,m) {
a = read(); b = read();
build(b,a);
}
solve1();
REP(i,n) printf("%d ",solve2(i));
return 0;
}