Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs
1 second
256 megabytes
standard input
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?
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.
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.
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
5
The following image represents possible process of removing leaves from the tree:
思路:dfs一波,找到sad的点,去掉sad为根的树;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9+;
ll a[N];
vector<pair<int,int > >edge[N];
int getnum(int u)
{
int ans=;
for(int i=;i<edge[u].size();i++)
ans+=getnum(edge[u][i].first);
return ans+;
}
int dfs(int u,ll dis)
{
int ans=;
for(int i=;i<edge[u].size();i++)
{
if(a[edge[u][i].first]<dis+edge[u][i].second)
ans+=getnum(edge[u][i].first);
else
ans+=dfs(edge[u][i].first,max(dis+edge[u][i].second,0LL));
}
return ans;
}
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
for(i=;i<=x;i++)
scanf("%d",&a[i]);
for(i=;i<x;i++)
{
int u,v;
scanf("%d%d",&u,&v);
edge[u].push_back(make_pair(i+,v));
}
int ans=dfs(,);
printf("%d\n",ans);
return ;
}
1 second
256 megabytes
standard input
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?
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.
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.
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
5
The following image represents possible process of removing leaves from the tree:
Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #358 (Div. 2) A B C 水 水 dfs序+dp
A. Alyona and Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #358 (Div. 2) E. Alyona and Triangles 随机化
E. Alyona and Triangles 题目连接: http://codeforces.com/contest/682/problem/E Description You are given ...
随机推荐
- Spark ML Pipeline简介
Spark ML Pipeline基于DataFrame构建了一套High-level API,我们可以使用MLPipeline构建机器学习应用,它能够将一个机器学习应用的多个处理过程组织起来,通过在 ...
- Spring框架第五篇之Spring与AOP
一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...
- yii2 商品上下架
视图层 <td><?php if($value['is_on_sale'] == 1) {?><img src="../web/images/yes.gif&q ...
- python介绍和基础(待补充)
python的介绍 把命令放到一个文件中,文件还能执行,这样的语言叫shell脚本 写一个c语言程序,.c结尾的,gcc运行c语言程序,生成.out文件,然后执行.out文件 c语言是先编写代码,再编 ...
- 11、classmethod和staticmethod
类中定义的函数有两大类(3小种)用途,一类是绑定方法,另外一类是非绑定方法 1. 绑定方法:特点:绑定给谁就应该由谁来调用,谁来调用就会将谁当作第一个参数自动传入1.1 绑定给对象的:类中定义的函数默 ...
- vue-cli脚手架build目录中的webpack.base.conf.js配置文件
转载自:http://www.cnblogs.com/ye-hcj/p/7082620.html webpack.base.conf.js配置文件// 引入nodejs路径模块 var path = ...
- UVALive - 4671 K-neighbor substrings (FFT+哈希)
题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串A和模式串B,求A中有多少与B海明距离<=k的不同子串 分析:将字符a视作1,b视作0.则A与B中都是a的位置乘积是1.现将B ...
- git 移除某个文件的版本管理
1:最简单的,在项目刚创建的时候,在根目录的.gitignore,加入该文件的相对路径 2:已经被纳入到了版本控制,使用在当前目录下,打开cmd窗口 输入rm命令,加上文件的绝对路径(相对路径没试过) ...
- C# DateTime 获取时间方法,网上收集
DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.T ...
- [2012-12-18 14:59:31]AS3 常用正则表达式的总结-不用google了,我帮收集的很多了
正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式,包括as3,这里转摘出了一些常用的正则表达式语句,大家用到的时候就不用自己写了.红色字体为常用正则:下面这个类是我自己写的一个示例,想必大 ...