Codeforces Round #358 (Div. 2) C. Alyona and the Tree (二叉树+DFS)

题目链接:点击打开链接

Description

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 v such that dist(v, u) > au, where au is the number written on vertex u, dist(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.

Sample 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

Sample Output

5

Hint

题意:

给你一棵树,有点权有边权。

如果存在两个点,u,v。满足u存在v的子树中,(u,v)的之间的边权和大于a[u]的话,那么u点是不开心的。

你只能从叶子节点开始删除点,问你最少删除多少个点,可以使得这个树里面没有不开心的点。

题解:

题目中的树是有顺序的,所以直接从1号节点dfs就好了,过程中用权值和与其父节点权值作比较取最大值,符合要求的点做标记

#include <bits/stdc++.h>
using  namespace  std;
typedef  long  long  ll;
const  int  maxn=1000005;
int head[maxn];
int mapp[maxn]; //记录结点权值
int vis[maxn];
int fre;
struct p{
   int node,next,cost;
}E[maxn];
void insertt(int x,int y,int cst){
    E[fre].node=y;
    E[fre].next=head[x];
    E[fre].cost=cst;
    head[x]=fre++;
}
void  dfs(int root ,ll sum){
    if(mapp[root]<sum)return;
    vis[root]=1;
    for(int  i=head[root] ; i!=-1 ; i=E[i].next){
        if(!vis[E[i].node]){
            dfs(E[i].node,max(sum+E[i].cost,(ll)E[i].cost));
        }
    }
}
int  main(){
//    #ifndef ONLINE_JUDGE
//        freopen("in.txt", "r", stdin);
//    #endif
    int  n,fre=0;
    scanf("%d",&n);
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
    for(int i=1 ; i<=n ; i++){
        scanf("%d",&mapp[i]);
    }
    for (int  i=1;i<n;i++){
       int  a,b;
       scanf("%d%d",&a,&b);
       insertt(a,i+1,b);
       insertt(i+1,a,b);
    }
    dfs(1,0);
    int num=0;
    for(int i=1 ; i<=n ; i++){
        if(vis[i])num++;
    }
    printf("%d\n",n-num);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值