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[],size[],vis[],ans;
vector<pii> g[]; void dfs(int now,int fa,int tot,int min1)
{
size[now]=;
min1=min(min1,tot);
if(tot-min1>a[now]) vis[now]=;
for(int i=;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=;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=;i<=n;i++)
{
scanf("%lld",&a[i]);
}
for(int i=;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(,,,);
dfs2(,);
printf("%lld\n",ans);
}

XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)的更多相关文章

  1. codeforces 682C Alyona and the Tree DFS

    这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...

  2. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  3. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

    D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  stan ...

  4. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  5. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  6. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  7. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  8. Codeforces 682C Alyona and the Tree

    题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...

  9. CodeForces 682C Alyona and the Tree(广搜 + 技巧)

    方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...

随机推荐

  1. python学习笔记(九):操作数据库

    我们在写代码的时候,经常会操作数据库,增删改查,数据库有很多类型,关系型数据库和非关系数据库,这里咱们介绍一下python怎么操作mysql.redis和mongodb. 一.python操作mysq ...

  2. Sqlmap用法小结

    一共有七个等级0.只显示python错误以及严重的信息.1.同时显示基本信息和警告信息.(默认)2.同时显示debug信息.3.同时显示注入的payload.4.同时显示HTTP请求.5.同时显示HT ...

  3. 转:Numpy教程

    因为用到theano写函数的时候饱受数据结构困扰 于是上网找了一篇numpy教程(theano的数据类型是基于numpy的) 原文排版更好,阅读体验更佳: http://phddreamer.blog ...

  4. 基于3D卷积神经网络的行为识别:3D Convolutional Neural Networks for Human Action Recognition

    简介: 这是一片发表在TPAMI上的文章,可以看见作者有余凯(是百度的那个余凯吗?) 本文提出了一种3D神经网络:通过在神经网络的输入中增加时间这个维度(连续帧),赋予神经网络行为识别的功能. 相应提 ...

  5. php call_user_func_array

    call_user_func_array (PHP >= , PHP , PHP ) call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 说明 mix ...

  6. [z]计算机架构中Cache的原理、设计及实现

    前言 虽然CPU主频的提升会带动系统性能的改善,但系统性能的提高不仅仅取决于CPU,还与系统架构.指令结构.信息在各个部件之间的传送速度及存储部件的存取速度等因素有关,特别是与CPU/内存之间的存取速 ...

  7. 【Oracle】Oracle数据库DATABASE LINK 的命名

    当前数据库的GLOBAL_NAMES参数设置为 TRUE,使用DATABASE LINK 时,DATABASE LINK的名称必须与被连接库的 GLOBAL_NAME一致. 而要建多个 DBLINK到 ...

  8. 跟着太白老师学python day10 名称空间,作用域和取值顺序,变量的加载顺序

    名称空间分为3种: 1. 全局名称空间 2. 内置名称空间 3. 局部名称空间(临时) 作用域 全局作用域              1全局名称空间 2 内置名称空间 局部作用域           ...

  9. Docker remote api 开启

    https://www.cnblogs.com/520playboy/p/7921633.html ExecStart=/usr/bin/dockerd-current -H unix:///var/ ...

  10. shell编程——变量子串的常用操作

    ${#字符串} 返回字符串的长度 [root@localhost ~]# a=length [root@localhost ~]# echo ${#a} 6 ${字符串:位置x} 从位置x开始往后截取 ...