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 ...
随机推荐
- 纯css3实现的switch开关按钮
效果如图 <p> <label><input class="mui-switch mui-switch-anim" type="checkb ...
- Sql Server并发和事务
锁的作用范围通常在事务中,事务是建立在并发模式下. 从SQL Server 2005开始,加入了一种新的并发模式-----乐观并发.不管使用哪种并发模式,如果多个会话同时修改相同的数据,都会产生资源争 ...
- ubuntu中vim下按上下左右键时输入A、B、C、D
ubuntu系统自带的 vi 不完整导致,解决方法:安装完整的vi $ sudo apt-get install vim-gtk 1. 为root用户设置密码 sudo passwd root 需要先 ...
- 使用Github Pages和Hexo构建个人博客
Github Pages可以创建免费的静态站点,支持自带主题.支持自制页面等,并且可以使用Jekyll或者Hexo等静态博客框架进行管理. 独立博客是博客园等博客平台之外的一个良好补充,相比使用虚拟主 ...
- Mafly.Mail实现发送邮件
安装 打开程序包管理器控制台,执行命令:Install-Package Mafly.MailInstall-Package Newtonsoft.Json.dll 安装之后,项目会自动创建一个Conf ...
- MySQL基础一(CMD使用)
概述 MySQL因可移植行高,安装简单小巧等优点被更多的开发者喜爱.执行MySQL的指令的方式有2种方式,方式一.MySQL的客户端软件比如navicat :方式二.通过Cmd命令: CMD命令执行方 ...
- [转]Maven与nexus关系
开始在使用Maven时,总是会听到nexus这个词,一会儿maven,一会儿nexus,当时很是困惑,nexus是什么呢,为什么它总是和maven一起被提到呢? 我们一步一步来了解吧. 一.了解Mav ...
- Android ADB命令教程二——ADB命令详解
Android ADB命令教程二——ADB命令详解 转载▼ 原文链接:http://www.tbk.ren/article/249.html 我们使用 adb -h 来看看,adb命令里面 ...
- BZOJ3926 [Zjoi2015]诸神眷顾的幻想乡 字符串 SAM
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3926.html 题目传送门 - BZOJ3926 题意 给定一个有 $n$ 个节点,最多只有 $20$ ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第十二集之FastDFS的使用】
(自己的项目路径)相关项目在web部分中priv.lirenhe.fastdfs 官方提供一个fastdfs开发项目,下载下来maven install 以下内容是借鉴网上的一篇文章,但是不知道网址了 ...