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 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:

比较有意思的一道题,刚开始题意理解错了,以为一定是从1开始,后来发现是最远可以是1,最近就是从父节点开始,那么边权和就变成了Max{0(舍弃之前的边从最近的父节点开始),sumpre(加上之前的边)}。然后再发现这道题也叫什么树形DP,反正也就听过的样子。一开始写BFS只能过两组数据,因为无法知道到底最合适起点在哪里。因此要用动态的思想,一开是BFS去计算减掉多少枝叶,看了别人博客才明白用逆向思维,因为你减掉的枝叶可能会重复,也可能这个枝叶根本不是从最优开始点开始计算的,也就无法得知这个节点到底算不算sad……题意是若中间出现了一个sad,那么这个sad点的后代点全部都要摘掉,因此去计算从1开始向外拓展的可保留点比较好,那显然就是DFS了。还有由于是无向图,边数要乘以2,两次RE

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=200010;
int r;
struct info
{
int to;
int pre;
int d;
}E[N];
int head[N],cnt,w[N],vis[N];
void add(int s,int t,int dx)
{
E[cnt].to=t;
E[cnt].pre=head[s];
E[cnt].d=dx;
head[s]=cnt++;
}
void init()
{
memset(head,-1,sizeof(head));
MM(w);MM(vis);
cnt=0;
r=0;
}
void dfs(const int &now,const int &pre,const LL &nowval)
{
if(w[now]<nowval||vis[now])
return ;
vis[now]=1;
r++;
for (int i=head[now]; i!=-1; i=E[i].pre)
{
int v=E[i].to;
dfs(v,now,max<LL>((LL)0,((LL)nowval+(LL)E[i].d)));
}
}
int main(void)
{
int n,i,j,x,y;
while (~scanf("%d",&n))
{
init();
for (i=1; i<=n; i++)
{
scanf("%d",&w[i]);
}
for (i=1; i<=n-1; i++)
{
scanf("%d%d",&x,&y);
add(i+1,x,y);
add(x,i+1,y);
}
dfs(1,1,(LL)0);
printf("%d\n",n-r);
}
return 0;
}

Codeforces Round #358 (Div. 2)——C. Alyona and the Tree(树的DFS+逆向思维)的更多相关文章

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

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

  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 #358 (Div. 2) C. Alyona and the Tree

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

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

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

  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. SQLite C/C++ 教程

    目录 1安装 2 C/C++ Interface APIs 3连接到数据库 4创建表 5插入操作 6更新操作 7删除操作 安装 在我们开始使用SQLite在C / C++程序,我们需要确保SQLite ...

  2. iOS,APP退到后台,获取推送成功的内容并且语音播报内容。

    老铁,我今天忙了一下午就为解决这个问题,网上有一些方法,说了一堆关于这个挂到后台收到推送并且获得推送内容的问题,有很多人都说APP挂到后台一会就被杀死.但实际上可以有办法解决的. WechatIMG3 ...

  3. hydra 中文文档

    hydra(九头蛇)是一款开源的协议爆破工具,功能十分强大!!! 具体使用如下: -R   继续从上一次进度接着破解 -I 忽略已破解的文件进行破解 -S 采用SSL链接 -s 端口 指定非默认服务端 ...

  4. faster rcnn细节总结

    1.roi_pooling层是先利用spatial_scale将region proposal映射到feature map上,然后利用pooled_w.pooled_h分别将映射后的框的长度.宽度等分 ...

  5. 制作新的train,test数据集

    之前的数据集的train和test是直接按照网上下载的数据的前7000个作为训练集,后2212个作为测试集.看得出来,这个数据集是由开车录制视频转换来的图片数据,后面2000多个图片的场景和前面的场景 ...

  6. python_111_动态导入模块

    lib下aa.py文件内容: class C: def __init__(self): self.name='alex' from lib import aa#正常导入 print(aa.C) 动态导 ...

  7. 解决VS2013无法安装ArcObjects10.2的问题

    之前在网上看到的10.1在vs2012安装不上的问题,解决办法是改注册表HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\1 ...

  8. Bootstrap 网格系统(Grid System)实例2

    Bootstrap 网格系统(Grid System):堆叠水平,两种样式 <!DOCTYPE html><html><head><meta http-equ ...

  9. shell脚本,awk实现行列转换

    [root@localhost study]# cat file 张三 语文 张三 数学 李四 语文 李四 数学 王五 语文 王五 数学 王五 英语 怎么实现为下面的排序??? 语文 数学 语文 数学 ...

  10. iOS开发--使用OpenSSL生成私钥和公钥的方法

    最近要在新项目中使用支付宝钱包进行支付,所以要调研对接支付宝的接口,支付宝开放平台采用了RSA安全签名机制,开发者可以通过支付宝   公钥验证消息来源,同时可使用自己的私钥对信息进行加密,所以需要在本 ...