Codeforces739B. Alyona and a tree(树+二分)

B. 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 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).

题意:一棵根节点为1的树,每个点都有点值a[i],每条边也有权值,dist(v, u)表示从v到u边权和,当u时v的子孙并且dist(v, u)<=a[u]时u就受v控制,输出每个结点能控制的结点数。

思路:如果v可以控制u,那么从v到u的路上的所有结点都可以控制u,因为从v到u路上的dist(vi, u)是递减的,可以每次遍历一个点的时候,二分找出根节点到当前点i路径上点,找出dist(j, i)刚好大于a[i]的点,使得ans[j]--,而后遍历当前点i的所有儿子结点k,ans[i] += ans[k]。

#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
#define ll long long int

vector<ll> val[maxn];
vector<int> G[maxn];
vector<pair<ll, int> > que;

ll dis[maxn], a[maxn];
int ans[maxn];
void dfs(int x, int fa){
    ans[x] = 1;
    ll t = dis[x] - a[x];
    int pos = lower_bound(que.begin(), que.end(), make_pair(t, 0))-que.begin();
    pos--;
    if(pos>=0) ans[que[pos].second]--;
    que.push_back(make_pair(dis[x], x));
    for(int i = 0;i < G[x].size();i++){
        int y = G[x][i];
        if(y == fa) continue;
        dis[y] = dis[x] + val[x][i];
        dfs(y, x);
        ans[x] += ans[y];
    }
    que.pop_back();
}
int main()
{
    int n, i;
    scanf("%d", &n);
    for(i = 1;i <= n;i++) scanf("%I64d", &a[i]);
    int v;
    ll cost;
    for(i = 2;i <= n;i++){
        scanf("%d %I64d", &v, &cost);
        val[i].push_back(cost);
        val[v].push_back(cost);
        G[i].push_back(v);
        G[v].push_back(i);
    }
    dfs(1, -1);
    for(i = 1;i <= n;i++) printf("%d%c", ans[i]-1, i==n?'\n':' ');
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值