【CF883B】Berland Army

题意:给出n个点,m条有向边,有的点的点权已知,其余的未知,点权都在1-k中。先希望你确定出所有点的点权,满足:

对于所有边a->b,a的点权>b的点权
对于i=1..k,至少有一个点的点权为i

n,m,k<=100000

题解:像菜肴制作一样奇怪的拓扑排序题,直接上方法吧,不会证。

先正反跑两边拓扑排序,得出每个点点权的下界Li和上界Ri。

将所有点按上界从小到大排序,然后枚举权值i。将所有上界为i的点都扔到堆中,再从堆里取出下界最大的那个点,将其权值赋为i。再找出所有下界为i的点,将他们的权值也都赋为i即可。

#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
#include <vector>
#define mp(A,B) make_pair(A,B)
using namespace std;
const int maxn=200010;
typedef pair<int,int> pii;
int n,m,k,cnt,flag;
int to[maxn],nxt[maxn],head[maxn],pa[maxn],pb[maxn],v[maxn],L[maxn],R[maxn],d[maxn],ans[maxn];
vector<int> p[maxn];
vector<int>::iterator it;
queue<int> q;
priority_queue<pii> pq;
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
inline void add(int a,int b)
{
to[cnt]=b,nxt[cnt]=head[a],head[a]=cnt++;
}
int main()
{
n=rd(),m=rd(),k=rd();
int i,u;
for(i=1;i<=n;i++)
{
v[i]=rd();
if(!v[i]) L[i]=1,R[i]=k;
else L[i]=R[i]=v[i];
}
memset(head,-1,sizeof(head)),cnt=0;
for(i=1;i<=m;i++) pa[i]=rd(),pb[i]=rd(),d[pb[i]]++,add(pa[i],pb[i]);
for(i=1;i<=n;i++) if(!d[i]) q.push(i);
while(!q.empty())
{
u=q.front(),q.pop();
for(i=head[u];i!=-1;i=nxt[i])
{
d[to[i]]--,R[to[i]]=min(R[to[i]],R[u]-1);
if(!d[to[i]]) q.push(to[i]);
}
}
for(i=1;i<=n;i++) if(d[i]) return puts("-1"),0;
memset(head,-1,sizeof(head)),cnt=0;
for(i=1;i<=m;i++) d[pa[i]]++,add(pb[i],pa[i]);
for(i=1;i<=n;i++) if(!d[i]) q.push(i);
while(!q.empty())
{
u=q.front(),q.pop();
for(i=head[u];i!=-1;i=nxt[i])
{
d[to[i]]--,L[to[i]]=max(L[to[i]],L[u]+1);
if(!d[to[i]]) q.push(to[i]);
}
}
for(i=1;i<=n;i++) if(d[i]||L[i]>R[i]) return puts("-1"),0;
for(i=1;i<=n;i++) p[R[i]].push_back(i);
for(i=k;i>=1;i--)
{
for(it=p[i].begin();it!=p[i].end();it++) pq.push(mp(L[*it],*it));
if(pq.empty()) return puts("-1"),0;
u=pq.top().second,pq.pop(),ans[u]=i;
while(!pq.empty())
{
u=pq.top().second;
if(L[u]<i) break;
pq.pop(),ans[u]=i;
}
}
for(i=1;i<=n;i++) printf("%d ",ans[i]);
return 0;
}

【CF883B】Berland Army 拓扑排序的更多相关文章

  1. 2017-2018 ACM-ICPC NEERC B题Berland Army 拓扑排序+非常伤脑筋的要求

    题目链接:http://codeforces.com/contest/883/problem/B There are n military men in the Berland army. Some ...

  2. Berland Army CodeForces - 883B (贪心,拓扑排序)

    大意: n个点, 点$i$的等级为$r_i$, 只给出部分点的$r$值, $r_i$的范围为[1,k], 且[1,k]都至少有一个. 给定m条有向边, (x,y)表示$r[x]>r[y]$, 求 ...

  3. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

  4. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  5. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  6. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  7. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  8. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  9. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

随机推荐

  1. 一键切换hosts文件

    1.新建文件host.bat 2.代码 @echo off cd.>C:\Windows\System32\drivers\etc\hosts echo .本地环境 .线上测试环境 ,切换Hos ...

  2. 【NLP】Stanford

    http://web.stanford.edu/class/cs224n/syllabus.html https://www.youtube.com/watch?v=OQQ-W_63UgQ&l ...

  3. linux echo命令

    该篇文章转载于:http://www.cnblogs.com/ZhangShuo/articles/1829589.html linux的echo命令, 在shell编程中极为常用, 在终端下打印变量 ...

  4. Activity 经历如下3个阶段

    Activity 经历如下3个阶段 开始Activity: 在这个阶段依次执行3个生命周期的方法,分别是onCreate onStart onResume 三个方法. Activity重新获取焦点:o ...

  5. 怎么解决BarTender因为未检测到IIS安装失败的问题

    个别小伙伴在安装BarTender条码标签设计软件的时候,遇到“未检测到IIS,无法安装BarTender Web Print Server配套程序”导致安装失败的问题,本文小编给大家分享解决BarT ...

  6. 源码分析六(org.springframework.util包之Assert类)

    一:抽象类Assert 抽象类不能够实例化对象,但是可以被继承,Assert类是功能类,所以方法都是static修饰 所以可以直接  类名.方法 调用. public abstract class A ...

  7. ajax+json+Struts2实现list传递(转载)

    一.首先需要下载JSON依赖的jar包.它主要是依赖如下: json-lib-2.2.2-jdk15 ezmorph-1.0.4       commons-logging-1.0.4       c ...

  8. Python_selenium二次封装selenium的几个方法

    Python_selenium二次封装selenium的几个方法 将常用的几个webdriver方法封装到自己写的一个类中去,此实例中是将"浏览器后退.浏览器前进.打开站点和关闭浏览器&qu ...

  9. [scala] scala 函数 (⑦)

    1.scala 函数定义 2.scala 高阶函数 3.匿名函数 4.柯里化 import scala.math._ /** * @author xwolf * @date 2017-04-24 9: ...

  10. gtk界面设计

    一.GTK基本 #include <gtk/gtk.h> int main( int argc, char *argv[]) { GtkWidget *window; /*初始化整个GTK ...