Codeforces - 682C Alyona and the Tree

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 vsad 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 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.

Sample Input

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

Hint

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

题意:

n个结点,第 i 个结点的权值为 ai , 有 m 条边,v u之间的权值为 dist(v, u)

对于任意的v来说,如果存在一个子树上的结点u,如果满足 dist(v, u) > au ,则该结点v是 sad

需要将 sad的结点去掉,求需要去掉多少个?(图为无向图)


思路:

从根节点(题目规定为1)向子树出发,每次维护一个从v到当前结点的最大值dist(v, u) 

依次搜索符合条件的结点

为什么要维护最大值?

解释: 1 - 2 - 3 - 4 (假设为无向图)

结点2:    1-2的距离为dist(1,2) ,1出发的只有一条边

结点3:    2-3的距离为 dist(2,3) ,1-3的距离为 dist(1,3) = dist(1,2) + dist(2,3)

              如果结点3需要满足条件的,那么需要满足 dist(2,3) <= v[3] && dist(1,3) <= v[3]

              那么只需要满足 dist(1,3) 与 dist(2,3) 之间的最大值 maxx <= v[3] 即可

因此,只需要每次记录下 1-3的最大值maxx,1-4为 sum =  maxx + dist(3,4),每次比较 sum 与 dist(3,4) 最大值即可
 

CODE:

链式前向星写法:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXX = 1e5+10;

struct NODE
{
    LL to;
    LL next;
    LL w;
}no[MAXX*2];
LL v[MAXX];
LL head[MAXX];
LL vis[MAXX];

void add(LL i,LL be,LL en,LL c)
{
    no[i].to = en;
    no[i].w = c;
    no[i].next = head[be];
    head[be] = i;
}

void DFS(LL x,LL maxx)
{
    LL sum,sum1;
    for(int i=head[x]; i!=-1; i=no[i].next)
    {
        if(vis[no[i].to] == 0){ // 没有被访问过
            sum = maxx + no[i].w;
            sum1 = max(sum,no[i].w); 
      // 需要注意这里,卡了几发,不能写成 maxx = max(sum,no[i].w) 
      // 如果写成这样的话,就修改了maxx的值,而在递归的时候,递归返回的时候仍然是递归开始的值
            if(sum1 <= v[no[i].to]){
                vis[no[i].to] = 1; // 需要标记
                DFS(no[i].to,sum1);
            }
        }
    }
}

int main()
{
    LL n,en,c;
    LL cnt = 0,ans = 0;
    memset(head,-1);
    scanf("%lld",&n);
    for(LL i=1; i<=n; i++)
        scanf("%lld",&v[i]);
    for(int i=2; i<=n; i++){
        scanf("%lld %lld",&en,&c);
        add(cnt++,i,en,c); // 无向边
        add(cnt++,en,i,c);
    }
    memset(vis,0);
    vis[1] = 1;
    DFS(1,0);
    for(LL i=1; i<=n; i++){
        if(vis[i] == 0)
            ans++;
    }
    printf("%lld\n",ans);
}

 

 

邻接表写法:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXX = 1e5+10;
vector<vector<LL> >vec(MAXX);
map<LL,map<LL,LL> > mapp; // 存无向边
LL v[MAXX];
LL vis[MAXX];

void DFS(LL x,LL maxx)
{
    LL sum,sum1;
    for(int i=0; i<vec[x].size(); i++){
        if(vis[vec[x][i]] == 0){
            sum = maxx + mapp[x][vec[x][i]];
            sum1 = max(sum,mapp[x][vec[x][i]]);
            if(sum1 <= v[vec[x][i]]){
                vis[vec[x][i]] = 1;
                DFS(vec[x][i],sum1);
            }
        }
    }
}
int main()
{
    LL n,ans = 0,en,c;
    scanf("%lld",&n);
    for(int i=1; i<=n; i++)
        scanf("%lld",&v[i]);
    for(int i=2; i<=n; i++){
        scanf("%lld %lld",&en,&c);
        vec[i].push_back(en);
        vec[en].push_back(i);
        mapp[i][en] = c;
        mapp[en][i] = c;
    }
    vis[1] = 1;
    DFS(1,0);
    for(int i=1; i<=n; i++)
        if(vis[i] == 0)
            ans++;
    printf("%lld\n",ans);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值