对于每条河流,要让它排名最靠前,那么显然它必须要延伸到根。

设第$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的更多相关文章

  1. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  2. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. [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 ...

  5. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  6. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  7. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  8. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  9. [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 ...

随机推荐

  1. JAVA 代码中使用中文的办法

    在编译代码中插入 -encoding UTF-8 示例: javac -encoding UTF-8 *.java

  2. 回到未来123Back To The Future

    或许,决定着现在的过去已经无法改变,但决定着未来的现在,却在我们每个人的手里. 路?我们要去的地方不需要路.(Roads? Where we're going we don't need roads) ...

  3. python字符串之join

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join():连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连 ...

  4. 从零开始学C#——数据类型(三)

    C#数据类型 在C#中,变量分为以下几种类型: 值类型 引用类型 指针类型 值类型 值类型变量可以直接分配给一个值,他们是从类System.ValucTpyc中派生. 值类型直接包含数据,比如int. ...

  5. WARN conf.FlumeConfiguration: Could not configure sink sink1 due to: No channel configured for sink: sink1 org.apache.flume.conf.ConfigurationException: No channel configured for sink: sink1

    1.错误如下所示,启动flume采集文件到hdfs案例的时候,出现如下所示的错误: 大概是说No channel configured for sink,所以应该是sink哪里配置出现了错误,百度了一 ...

  6. [转] JavaScript 单例模式

    定义 确保一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式使用的场景 比如线程池.全局缓存等.我们所熟知的浏览器的window对象就是一个单例,在JavaScript开发中,对于这种只需要 ...

  7. Eclipse+Maven整合开发Java项目(一)➣Maven基础环境配置

    概述 Maven是一个Java语言编写的开源项目管理工具,是Apache软件基金会的顶级项目.主要用于项目构建,依赖管理,项目信息管理.有些项目需要添加响应的依赖包,Maven就是公用包集合.存在远程 ...

  8. POJ1469 COURSES 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2 ...

  9. springmvc 无法访问静态资源

    没有配置<mvc:resources location="/" mapping="/**"/> <?xml version="1.0 ...

  10. IDEA创建lo4j模板

    复制文字到文本框中: log4j.rootLogger=DEBUG,stdout log4j.logger.org.mybatis=DEBUG log4j.appender.stdout=org.ap ...