Codeforces 740D dfs+二分

本文介绍了一个关于树结构的问题——Alyona与树,其中涉及到树的遍历、区间查询等算法,并提供了一种使用DFS和二分查找来解决掌控点计数问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alyona and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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 ≤ n1 ≤ 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 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值