博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Alyona and a tree CodeForces - 740D
阅读量:4135 次
发布时间:2019-05-25

本文共 3056 字,大约阅读时间需要 10 分钟。

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 that v controls u.

Input

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.

Output

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Examples

Input
5
2 5 1 4 6
1 7
1 1
3 5
3 6
Output
1 0 1 0 0
Input
5
9 7 8 6 5
1 1
2 1
3 1
4 1
Output
4 3 2 1 0
Note
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 1 controls the vertex 5).
题意:给出一棵树,要你求出这棵树上的每个节点能控制多少个节点。控制节点的定义为:dis(u,v)<=a[u]。
思路:对于一棵树上的点来说,随着深度的深入,距离根节点上的距离是越来越大的,满足单调性。如果说dis(u,v)<=a[u],那么dis[u]-dis[v]<=a[u],那么dis[u]-a[u]<=dis[v].那么我们可以二分去找大于等于dis[u]+a[u]的第一个点。找到的这个点到当前点父亲节点的控制点数就增加1。然后dfs倒序就可以求出所有节点的控制点数。
代码如下:

#include
#define ll long longusing namespace std;const int maxx=2e5+100;struct edge{
int to,next; ll w;}e[maxx<<1];struct node{
int u; ll v; node(int a,ll b) {
u=a,v=b; } bool operator<(const node &a)const{
return a.v>v; }};int head[maxx<<1],a[maxx],ans[maxx];vector
v;ll dis[maxx];int n,tot;/*--------------事前准备----------------*/inline void init(){
v.clear(); memset(head,-1,sizeof(head)); memset(ans,0,sizeof(ans)); tot=0;}inline void add(int u,int v,ll w){
e[tot].to=v,e[tot].next=head[u],e[tot].w=w,head[u]=tot++;}/*--------------dfs+二分---------------*/inline void dfs(int u,int f){
ll cnt=dis[u]-a[u]; int pos=lower_bound(v.begin(),v.end(),node(0,cnt))-v.begin();//插进去的值一定是有序的。 ans[f]++;//父亲节点开始起作用 if(pos>0) ans[v[pos-1].u]--;//这个点之前的点-1,差分思想。 v.push_back(node(u,dis[u]));//将这个点压入vector for(int i=head[u];i!=-1;i=e[i].next) {
int to=e[i].to; if(to==f) continue; dis[to]=dis[u]+e[i].w; dfs(to,u); ans[u]+=ans[to]; } v.pop_back();//把当前点pop掉。}int main(){
int x,y;ll w; while(~scanf("%d",&n)) {
init(); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=2;i<=n;i++) {
scanf("%d%lld",&x,&w); add(x,i,w); add(i,x,w); } dis[0]=0; dfs(1,0); for(int i=1;i<=n;i++) printf("%d%c",ans[i],i==n?'\n':' '); } return 0;}

还是得多想。

努力加油a啊,(o)/~

转载地址:http://gztvi.baihongyu.com/

你可能感兴趣的文章
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
第七章 背包问题——完全背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
State模式
查看>>
Observer模式
查看>>
Iterator模式
查看>>
中国最完整的sysctl.conf优化方案
查看>>
高性能服务器设计
查看>>
性能扩展问题要趁早
查看>>
领域对象的生命周期
查看>>
模式:AGGREGATE
查看>>
模式:FACTORY
查看>>
Vue部署后刷新页面出现404
查看>>
MySQL-数据库、数据表结构操作(SQL)
查看>>