跳到正文

题解

题解:NOI2010 航空管制

gyh20约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年6月14日
更新
2026年6月14日
篇幅
约 3 分钟
获赞
16
题目
P1954

版本

  1. v1当前

做题时偶然想到的一个复杂度更优的做法,写一下题解。

首先第一问,建反图做类似于 HNOI2015 菜肴制作的贪心,即可构造一种方案。

第二问大家很多 O(nm+n2log⁡n)O(nm+n^2\log n) 的题解都是之后枚举一个点强制不选,贪心的选其他点,重新做一次拓扑排序,直到出现不合法情况说明必须使用当前点,每次拓扑排序是 O(m+nlog⁡n)O(m+n\log n) 的。

可以发现,假设当前枚举点是 xx,那么所有在 DAG 上可以由 xx 到达的点都永远无法到达,但是对于其他点的相对拓扑序没有改变,可直接枚举原来的拓扑序,再强制删掉由 xx 可达的点。

而找到 DAG 上可以由 xx 到达的点这一步可以用 bitset 优化,做到总复杂度 O(n2+nmω)O(n^2+\frac{nm}{\omega}),是目前的 Rank 1。

C++
#include<bits/stdc++.h>
#define re register
using namespace std;
int n,p[2002],head[2002],cnt,m,a[2002],d[2002];
struct edge{int to,next;}e[10002];
bitset<2002>B[2002];
inline void add(re int x,re int y){e[++cnt]=(edge){y,head[x]},head[x]=cnt,++d[y];}
struct node{int x,y;bool operator <(const node a)const{return y<a.y;}};
priority_queue<node>q;
inline int calc(re int z){
	cnt=0;
	for(re int i=1;i<=n;++i){
		re int x=p[i];
		if(B[x][z])continue;
		++cnt;
		if(a[x]<=n-cnt)return n-cnt+1;
	}
	return n-cnt;
}
inline int read(){
	re int t=0;re char v=getchar();
	while(v<'0')v=getchar();
	while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
	return t;
}
signed main(){
	n=read(),m=read();
	for(re int i=1;i<=n;++i)a[i]=read();
	for(re int i=1,x,y;i<=m;++i)x=read(),y=read(),add(y,x);
	for(re int i=1;i<=n;++i)if(!d[i])q.push((node){i,a[i]});cnt=0;
	while(!q.empty()){
		re int x=q.top().x;q.pop();p[++cnt]=x;
		for(re int i=head[x];i;i=e[i].next)if(!(--d[e[i].to]))q.push((node){e[i].to,a[e[i].to]});
	}
	for(re int i=1;i<=n;++i){
		re int x=p[i];
		B[x][x]=1;
		for(re int j=head[x];j;j=e[j].next)B[e[j].to]|=B[x];
	}
	for(re int i=n;i;--i)printf("%d ",p[i]);puts("");
	for(re int i=1;i<=n;++i)printf("%d ",calc(i));
}
原文
存档评论5 条抓取于