C. Alyona and the Tree
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

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是在v的子树中的节点,那么当d(u,v)>ans[u]时此时v就是不开心的节点。然后让你删除节点,删除节点后使得剩下的

点都为开心点,让你求最少删除的点。

思路:dfs+dp;

从根节点深搜下去,然后dp[i]表示在这个节点上边并能到达这个点的最大值,那么当dp[i]>ans[i]时就表示当前这个节点,以及下面所有的子节点都要删除。

最后统计下没被遍历到的和被标记的。

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<math.h>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 LL ans[100006];
12 typedef struct pp
13 {
14 int id;
15 LL cost;
16 } ss;
17 void dfs(int n);
18 bool flag[100006];
19 LL dp[100006];
20 vector<ss>vec[100006];
21 int main(void)
22 {
23 int i,j,k;
24 while(scanf("%d",&k)!=EOF)
25 {
26 fill(dp,dp+100006,-1e16);
27 dp[1]=0;
28 for(i=1; i<=k; i++)
29 {
30 scanf("%lld",&ans[i]);
31 }
32 for(i=0; i<100006; i++)
33 vec[i].clear();
34 memset(flag,0,sizeof(flag));
35 for(i=0; i<k-1; i++)
36 {
37 int n;
38 LL m;
39 scanf("%d %lld",&n,&m);
40 ss ans;
41 ans.cost=m;
42 ans.id=i+2;
43 vec[n].push_back(ans);
44 }
45 dfs(1);
46 int remoe=0;
47 for(i=1; i<=k; i++)
48 {
49 if(flag[i])
50 {
51 remoe++;
52 }
53 if(dp[i]==-1e16)
54 remoe++;
55 }
56 printf("%d\n",remoe);
57 }
58 return 0;
59 }
60 void dfs(int n)
61 {
62 int i,j,k;
63 for(i=0; i<vec[n].size(); i++)
64 {
65 ss ak=vec[n][i];
66 int id=ak.id;
67 dp[id]=max(dp[id],dp[n]+ak.cost);
68 dp[id]=max(ak.cost,dp[id]);
69 if(dp[id]>ans[id])
70 {
71 flag[id]=true;
72 }
73 else
74 {
75 dfs(id);
76 }
77 }
78 }

最新文章

  1. WebService初学
  2. [ES] 安装
  3. HTML5的Server-Sent Events介绍
  4. Hlsl2glsl
  5. HDU 3642 Get The Treasury 线段树+分层扫描线
  6. java transient关键字和transaction的区别
  7. 《UNIX环境高级编程》笔记--文件共享
  8. svn: E200007: CHECKOUT can only be performed on a version resource
  9. 音频压缩编码 opus 附完整C++代码示例
  10. BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP
  11. input 上报流程图
  12. AtCoder Grand Contest 031 (AGC031) D - A Sequence of Permutations 其他
  13. python基础之Day21
  14. 第一册:lesson ninety one.
  15. 我的ecshop二次开发经验分享
  16. 前后端同学必会的Linux基础命令
  17. PCH Warning: header stop cannot be in a macro or #if block.
  18. Android Studio和SDK下载、安装和环境变量配置
  19. vb.net 模拟UDP通信
  20. HDU 5234 Happy birthday 01背包

热门文章

  1. 详解工作流框架Activiti的服务架构和组件
  2. 日常Java 2021/10/4
  3. A Child&#39;s History of England.35
  4. HDFS【Java API操作】
  5. Docker学习(二)——Docker容器使用
  6. RTTI (Run-time type information) in C++
  7. NSString类里有个hash
  8. shell 截取字符串实例教程
  9. 【spring AOP】AspectJProxyFactory
  10. linux 让.net 控制台后台运行