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

设第$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. 步步为营-94-GridView中的DropDownlist值得获取与绑定

    bug场景: 例如这种"计税方式"是下拉列表的,当选择"编辑"时候,数据会丢失 修改方式,前台对应修改 后台代码在databound时候给绑定值 测试效果

  2. 【loj6029】「雅礼集训 2017 Day1」市场&&【uoj#228】基础数据结构练习题

    题解: 这两道题加上区间取min max应该算线段树几道比较不寻常的题目 其实也是挺好理解的 对于区间/d 显然在log次后就会等于0 而我们注意到如果区间中数都相等那么就可以一起除 也就是说每个区间 ...

  3. request.user哪里来的?

    1.登录认证(auth认证登录后login后设置了session等信息包含用户的pk)      >>>>>               2.用户再次请求登录的时候,通过 ...

  4. angularjs 中通过 $location 进行路由跳转传参

    $location.path('/page1').search({id: $scope.id,name:$scope.name}); 带参数跳转页面,在新的页面通过$routeParams接收参数 $ ...

  5. 高性能之html

    下面是alloyteam的总结,还是那句老话站在巨人的肩膀上看的远. 避免使用Iframe Iframe也叫内联frame,可以把一个HTML文档嵌入到另一个文档中.使用iframe的好处是被嵌入的文 ...

  6. fillder--信息面板展示serverIP

    1.Ctrl+R打开面板 2.如上图的位置,加上一句后,重启Fillder即可 FiddlerObject.UI.lvSessions.AddBoundColumn(, "X-HostIP& ...

  7. 求小于n且与n互质的数的个数

    int eu(int n){ int ans=n; for(int i=2;i*i<=n;i++) { if(n%i==0) { ans=ans/i*(i-1); while(n%i==0)n/ ...

  8. 查看name的状态,是属于active还是standby

    sudo -E -u hadoop /home/hadoop/bin/hdfs haadmin -getServiceState nn1 sudo -E -u hadoop /home/hadoop/ ...

  9. P1135 奇怪的电梯 dp

    题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第ii层楼(1 \le i \le N)(1≤i≤N)上有一个数字K_i(0 \le K_i \le N)K ...

  10. 20165235 祁瑛 2018-4 《Java程序设计》第八周学习总结

    20165235 祁瑛 2018-4 <Java程序设计>第八周学习总结 教材学习内容总结 操作系统与进程 程序是一段静态的代码,它是应用软件执行的蓝本.进程是程序的一次动态执行过程,它对 ...