Alyona and a tree CodeForces - 740D

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<bits/stdc++.h>
#define ll long long
using 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<node> 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)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值