XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)

Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on.

The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex v sad if there is a vertex u in subtree of vertex vsuch that dist(v, u) > au, where au is the number written on vertex udist(v, u) is the sum of the numbers written on the edges on the path from v to u.

Leaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex — root.

Thus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove?

Input

In the first line of the input integer n (1 ≤ n ≤ 105) is given — the number of vertices in the tree.

In the second line the sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 109) is given, where ai is the number written on vertex i.

The next n - 1 lines describe tree edges: ith of them consists of two integers pi and ci (1 ≤ pi ≤ n - 109 ≤ ci ≤ 109), meaning that there is an edge connecting vertices i + 1 and pi with number ci written on it.

Output

Print the only integer — the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.

Example

Input
9
88 22 83 14 95 91 98 53 11
3 24
7 -8
1 67
1 64
9 65
5 12
6 -80
3 8
Output
5

Note

The following image represents possible process of removing leaves from the tree:

题意:

给你一棵树,n个点, 以1为根,每个点有一个值,每条边有边权

假如一个点u是悲伤的,那么u的子树中存在一个点v,使得dist(u,v)>a[v]dist(u,v)表示u 到 v的路径上的边权之和, a[v] 是v的点权

每次操作你可以删掉一个叶子节点,问你至少操作几次使得所有的节点都不再悲伤。

                                                                                                                                      ---translate by 万古神犇517

题解:

这题大约有个结论:如果一个点v是使u悲伤的,那么这个点一定要移掉,如果要移掉这个点,必须把这个点的整个子树都移掉

显然确认一个点是不是要被移掉仅与他的权值和到所有祖先节点的dist有关,这个东西是可以O(n)DFS出来的

u->v的距离其实就相当于1-v的距离减去1-u的距离,因为只需要一个最大距离,所以只需要记录最小的1-u就可以了

代码如下:

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pii pair<int,int>
#define mp make_pair
#define int long long
using namespace std;
 
int n,a[100010],size[100010],vis[100010],ans;
vector<pii> g[100010];
 
void dfs(int now,int fa,int tot,int min1)
{
    size[now]=1;
    min1=min(min1,tot);
    if(tot-min1>a[now]) vis[now]=1;
    for(int i=0;i<g[now].size();i++)
    {
        int y=g[now][i].first;
        int w=g[now][i].second;
        if(y==fa) continue;
        dfs(y,now,tot+w,min1);
        size[now]+=size[y];
    }
}
 
void dfs2(int now,int fa)
{
    if(vis[now])
    {
        ans+=size[now];
        return ;
    }
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i].first==fa) continue;
        dfs2(g[now][i].first,now);
    }
}
 
signed main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    for(int i=2;i<=n;i++)
    {
        int to,cost;
        scanf("%lld%lld",&to,&cost);
        g[to].push_back(mp(i,cost));
        g[i].push_back(mp(to,cost));
    }
    dfs(1,0,0,0);
    dfs2(1,0);
    printf("%lld\n",ans);
}

猜你喜欢

转载自www.cnblogs.com/stxy-ferryman/p/9321413.html