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 }

Codeforces Round #358 (Div. 2) C. Alyona and the Tree的更多相关文章

  1. Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题

    C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...

  2. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  5. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  6. Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)

    D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...

  7. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

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

  8. Codeforces Round #358 (Div. 2) E. Alyona and Triangles 随机化

    E. Alyona and Triangles 题目连接: http://codeforces.com/contest/682/problem/E Description You are given ...

  9. Codeforces Round #358 (Div. 2) D. Alyona and Strings dp

    D. Alyona and Strings 题目连接: http://www.codeforces.com/contest/682/problem/D Description After return ...

随机推荐

  1. Deep Learning(深度学习)整理,RNN,CNN,BP

     申明:本文非笔者原创,原文转载自:http://www.sigvc.org/bbs/thread-2187-1-3.html 4.2.初级(浅层)特征表示 既然像素级的特征表示方法没有作用,那怎 ...

  2. 各个浏览器的webdriver

    Chrome 点击下载chrome的webdriver: http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的ch ...

  3. 巩固javaweb的第二十天

    巩固内容: 同一个页面中的多个 form 在同一个页面中可以有多个 form 如果存在多个 form,那么提交信息的时候提交哪些信息,提交给哪个文件处理,这都 与提交按钮的位置有关.如果提交按钮在第一 ...

  4. 备忘录:关于.net程序连接Oracle数据库

    目录 关于使用MSSM访问Oracle数据库 关于. net 程序中连接Oracle数据库 志铭-2021年12月7日 21:22:15 关于使用MSSM访问Oracle数据库 安装访问接口组件:Or ...

  5. k8s使用ceph的rbd作后端存储

    k8s使用rbd作后端存储 k8s里的存储方式主要有三种.分别是volume.persistent volumes和dynamic volume provisioning. volume: 就是直接挂 ...

  6. 【leetcode】451. Sort Characters By Frequency

    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...

  7. Linux基础命令---ntpdate网络时间服务器

    ntpdate ntpdate指令通过轮询指定为服务器参数的网络时间协议(NTP)服务器来设置本地日期和时间,从而确定正确的时间. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS ...

  8. redis安装与简单实用

    1.在Linux上redis的安装时十分简单的: 第一步:wget http://download.redis.io/releases/redis-2.8.12.tar.gz 解压: tar zxvf ...

  9. iOS UIWebview 长按图片,保存到本地相册

    我们所要解决的问题如题目所示:ios中,长按Webview中的图片,将图片保存到本地相册.解决方案:对load的html网页,执行js注入,通过在webview中执行js代码,来响应点击事件,通过js ...

  10. Java易错小结

    String 相关运算 String使用是注意是否初始化,未初始化的全部为null.不要轻易使用 string.isEmpty()等,首先确保string非空. 推荐使用StringUtils.isN ...