Codeforces 740D dfs+二分
来源:互联网 发布:淘宝怎样取消退款 编辑:程序博客网 时间:2024/05/21 10:40
Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).
Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.
The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.
Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such thatv controls u.
The first line contains single integer n (1 ≤ n ≤ 2·105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.
The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).
It is guaranteed that the given graph is a tree.
Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.
52 5 1 4 61 71 13 53 6
1 0 1 0 0
59 7 8 6 51 12 13 14 1
4 3 2 1 0
In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1controls the vertex 5).
题意:对于两个点 如果dis(u,v)<=val(v) 且v是u的子孙 则称u掌控v 现在求每个结点掌控点的个数
题解:从1结点开始dfs 开一个栈 保存dis和lab
所以dfs到任意一个结点时 因为栈里面的dis是单调递增的 所以二分找出来第一个能掌控的点 然后这个点到当前点的父亲的掌控数都+1
现在具体说一下二分
假设现在的树只是一条线 当前dfs到v 栈里面存的就是
dis(1,1),1 dis(1,2),2 dis(1,3),4 dis(1,4),4 ..... dis(1,v),v
所以要使得u能掌控v
val(v)>=dis(u,v) dis(u,v)=dis(1,v)-dis(1,u)
所以只要用lowerbound找dis(1,v)-val(v)的第一个位置即可
更新的时候也不需要用线段树更新 在开始点的标记+1 在当前点的标记-1 然后从下往上travel即可
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;ll val[200005],head[200005],cnt=0,ans[200005],num=0,cha[200005];struct point{ll num,lab;bool operator <(const point& a)const{return num<a.num;}};point q[200005];struct node{ll to,nex,cap;}edge[200005];void add(ll u,ll v,ll w){edge[cnt].to=v;edge[cnt].cap=w;edge[cnt].nex=head[u];head[u]=cnt++;}void dfs(ll t,ll ss){ll dd=lower_bound(q+1,q+1+num,(point){ss-val[t],1})-q;if(dd<=num){cha[t]++;cha[q[dd].lab]--;}q[++num]=(point){ss,t};ll i;for(i=head[t];~i;i=edge[i].nex){ll v=edge[i].to;dfs(v,ss+edge[i].cap);num--;}}ll travel(ll t){if(!(~head[t]))return cha[t];for(ll i=head[t];~i;i=edge[i].nex){ans[t]+=travel(edge[i].to);}return ans[t]+cha[t];}int main(){int n,i,j,x,y;scanf("%lld",&n);memset(head,-1,sizeof(head));for(i=1;i<=n;i++)scanf("%lld",&val[i]);for(i=2;i<=n;i++){scanf("%lld%lld",&x,&y);add(x,i,y);}dfs(1,0);travel(1);for(i=1;i<=n;i++)printf("%lld ",ans[i]);printf("\n");return 0;}
- Codeforces 740D dfs+二分
- 【Codeforces 570D 】Tree Requests dfs序+二分
- Codeforces Round #316 (Div. 2) D 【dfs序+二分】
- Codeforces Round #316 (Div. 2) D DFS+vector+二分
- [Codeforces #316 D. Tree Requests]DFS序、离线、二分
- Codeforces 366D Dima and Trap Graph【二分+Dfs】
- CodeForces 645 D.Robot Rapping Results Report(二分+dfs)
- Codeforces 570D Tree Requests【Dfs序+二分】好题!
- codeforces 598D dfs
- 【codeforces 723D 】 + DFS
- CodeForces 327D(DFS)
- codeforces 567D 二分
- codeforces 672D 二分
- Codeforces 492d 二分
- Codeforces 732D 二分
- codeforces 779D 二分
- Codeforces Round #316 (Div. 2)-D. Tree Requests-DFS+二分+hash
- *Codeforces Round #316 (Div. 2)- D. Tree Requests (dfs+二分)
- UML类图中箭头和线条的含义和用法
- 监听键盘高度oc---swfit
- MongoDB 导出导入备份恢复数据实例
- 包含IN子查询的SQL语句的优化
- 带有自动换行的报表补足空行的实现方法
- Codeforces 740D dfs+二分
- 如何获取color.xml配置文件中的值。
- c++中冒号(:)和双冒号(::)的用法
- 子字符串查找
- dubbo新人帮助文档
- lame静态库使用
- android socket网络通信
- java中的日期类
- 微信公众平台开发(好文章 好博客链接)