BZOJ4613 : [Wf2016]Longest Rivers
对于每条河流,要让它排名最靠前,那么显然它必须要延伸到根。
设第$i$条河流到根的距离为$d[i]$,对于每个节点,如果存在一条河流比$d[i]$长,那么让它延伸会使答案最小,否则要选择一条最短的河流来进行延伸。
设$f[i]$表示每个节点往外延伸的河流的长度的最小值,可以通过树形DP求出。
从小到大考虑每个$d$,取出所有超过了上一个$d$的限制,但是满足当前的$d$限制的临界点,这些点将不再是临界点。
若一个点所有的儿子都不是临界点,那么它的将变为临界点。
用堆按$f$从小到大维护临界点即可,答案就是临界点个数$+1$,也就是堆中元素个数$+1$。
因为每个点只会进堆、出堆各一次,所以时间复杂度为$O(n\log n)$。
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,int>P;
const int N=500010,M=1000010;
const ll inf=1LL<<60;
char name[N][12];
int n,m,cnt,i,x,y,w[M],g[M],nxt[M],fa[M],deg[M],a[N],ans[N];ll d[M],f[M];
priority_queue<P,vector<P>,greater<P> >q;
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline bool cmp(int x,int y){return d[x]<d[y];}
inline void add(int x,int y){nxt[y]=g[x];g[x]=y;}
void dfs(int x){
f[x]=x<=n?0:inf;
for(int i=g[x];i;i=nxt[i]){
d[i]=d[x]+w[i];
fa[i]=x;
dfs(i);
if(f[i]<f[x])f[x]=f[i];
deg[x]++;
}
f[x]+=w[x];
}
inline void up(int x){
if(x==n+1)return;
if(--deg[x])return;
cnt++;
q.push(P(f[x],x));
}
int main(){
read(n),read(m);
for(i=1;i<=n;i++){
scanf("%s",name[i]);
read(x),read(w[i]);
add(x+n+1,i);
}
for(i=1;i<=m;i++)read(x),read(w[i+n+1]),add(x+n+1,i+n+1);
dfs(n+1);
f[n+1]=inf;
for(i=1;i<=n;i++)a[i]=i;
sort(a+1,a+n+1,cmp);
cnt=n;
for(i=1;i<=n;i++)q.push(P(f[i],i));
for(i=1;i<=n;ans[a[i++]]=cnt)while(!q.empty()){
P t=q.top();
if(t.first>d[a[i]])break;
q.pop();
cnt--;
up(fa[t.second]);
}
for(i=1;i<=n;i++)printf("%s %d\n",name[i],ans[i]+1);
return 0;
}
BZOJ4613 : [Wf2016]Longest Rivers的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
随机推荐
- ubuntu下配置反向代理
1. 环境 ubuntu:Ubuntu 13.04 x86-64 apache2: 2.2.22-6ubuntu5.1 amd64 2. 配置 2.1 配置应用 增加监听端口 打开/etc/apac ...
- Lua中,泛型for循环遍历table时,ipairs和pairs的区别
为了看出两者的区别,首先定义一个table: a={"Hello","World";a=1,b=2,z=3,x=10,y=20;"Good" ...
- 008-Python-模块
1.模块 1.1什么是模块 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀: 模块分为: 内置模块(内部定义的如time,os,sys等) 第三方模块(需要安装 ...
- python内置的魔术命令(builtin magic commands)
在ipython或者jupyter notebook中,会出现"%"开头并且一个很短的命令,例如交互式的matlablib绘图: %matplotlib inline 之前一直不知 ...
- MyBatis - 1.入门
MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyBatis可以使用简单的XML或注解 ...
- ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch
https://www.cnblogs.com/wt645631686/p/8274722.html
- 开始写博客,学习Linq(1)
摘自<linq实战>原文: 软件很简单.它可以归结为两件事情:代码和数据. 开发软件却并非那么简单,其中很重要的一项任务就是编写处理数据的代码. 无论选择了哪种语言,在程序开发得某个时候你 ...
- EF分别使用IQueryable和IEnumerable实现更新和删除
缺点 使用IQueryable无法跟踪,无法监控sql,无法使用SaveChanges(). 优点 使用IQueryable简单粗暴. class Program { static void Main ...
- [转] 深入浅出mongoose-----包括mongoose基本所有操作,非常实用!!!!!
深入浅出mongoose mongoose是nodeJS提供连接 mongodb的一个库. 此外还有mongoskin, mongodb(mongodb官方出品). 本人,还是比较青睐mongoose ...
- JSP中out.print()、out.println()以及out.write()的区别
out是JSP九大内置对象之一,是JspWriter的一个对象,JspWriter继承了java.io.Writer类. out.print()和out.write() print()和println ...