首页 技术 正文
技术 2022年11月15日
0 收藏 848 点赞 2,279 浏览 1477 个字

有个容易混的概念就是第一问的答案不是k[i]字典序最小即可,是要求k[i]大的尽量靠后,因为这里前面选的时候是对后面有影响的(比如两条链a->b c->d,ka=4,kb=2,kc=3,kd=4,按字典序就先选c然后b就不能合法了)

所以倒着来,建反图,然后按照n-k[i]从大到小拓扑,因为是反图所以是k大的尽量靠后

然后考虑第二问,是当前点x在拓扑中能入队先不入,直到某个点非法再入,这样虽然顺序变了但是非法点的排名不变所以依然合法

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=2005;
int n,m,a[N],h[N],cnt,c[N],d[N],ans[N],tot;
struct qwe
{
int ne,to;
}e[N*10];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
inline int wk(int x)
{
tot=0;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
for(int i=1;i<=n;i++)
d[i]=c[i];
for(int i=1;i<=n;i++)
if(!d[i])
q.push(make_pair(n-a[i],i));
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(u==x)
continue;
if(n-tot>a[u])
return n-tot;
tot++;
for(int i=h[u];i;i=e[i].ne)
if(!(--d[e[i].to]))
q.push(make_pair(n-a[e[i].to],e[i].to));
}
return n-tot;
}
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();
add(y,x);
d[x]++;
}
for(int i=1;i<=n;i++)
c[i]=d[i];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
for(int i=1;i<=n;i++)
if(!d[i])
q.push(make_pair(n-a[i],i));
while(!q.empty())
{
int u=q.top().second;
q.pop();
ans[++tot]=u;
for(int i=h[u];i;i=e[i].ne)
if(!(--d[e[i].to]))
q.push(make_pair(n-a[e[i].to],e[i].to));
}
for(int i=n;i>=1;i--)
printf("%d ",ans[i]);
puts("");
for(int i=1;i<=n;i++)
printf("%d ",wk(i));
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298