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 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.
Sample 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
Sample Output
5
Hint
题意
给你一棵树,有点权有边权。
如果存在两个点,u,v。满足u存在v的子树中,(u,v)的之间的边权和大于a[u]的话,那么u点是不开心的。
你只能从叶子节点开始删除点,问你最少删除多少个点,可以使得这个树里面没有不开心的点。
题解:
题目中的树是有顺序的,所以直接从1号节点dfs就好了
如果要删除u点的话,那么显然u子树也得全部删去,所以直接dfs一波就完了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+6;
vector<pair<int,int> >E[maxn];
int a[maxn];
int dfs(int x,int fa,long long dis)
{
if(dis>a[x])return 0;
long long ans = 1;
for(int i=0;i<E[x].size();i++)
{
if(E[x][i].first==fa)continue;
ans+=dfs(E[x][i].first,x,max(dis+E[x][i].second,0LL));
}
return ans;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int p,c;
for(int i=1;i<=n-1;i++)
{
scanf("%d%d",&p,&c);
E[i+1].push_back(make_pair(p,c));
E[p].push_back(make_pair(i+1,c));
}
int ans = dfs(1,0,0);
cout<<n-ans<<endl;
}
Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题的更多相关文章
- 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(树的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 #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- Codeforces Round #358 (Div. 2)B. Alyona and Mex
B. Alyona and Mex time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 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 树上二分+前缀和思想
题目链接: 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 #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 ...
随机推荐
- spark和hadoop比较
来源知乎 计算模型:hadoop-MapReduce,Spark-DAG(有向无环图)评注:经常有人说Spark就是内存版的MapReduce,实际上不是的.Spark使用的DAG计算模型可以有效的减 ...
- 日期时间设置 "2018-05-04T16:36:23.6341371+08:00" 格式
using System;using System.Collections.Generic;using System.Globalization;using System.Text; namespac ...
- 蛮力法解决0_1背包问题新思路-——利用C语言位域类型
废话不说了,直接上代码 #include<stdio.h> #include<math.h> #define N 5 //物品种类数目 #define CAPACITY 6 / ...
- echarts一些笔记
console.log(); 浏览器显示 $.ajax({ url : "ajax/echartWelcome.action", type : "post", ...
- MySQL权限问题
1.修改MySQL用户密码 .先来看一个PASSWORD()函数,MYSQL使用MD5加密 SELECT PASSWORD(‘root’); .使用mysql数据库,查看用户表 USE mysql; ...
- 【读书笔记】Android的Ashmem机制学习
Ashmem是安卓在linux基础上添加的驱动模块,就是说安卓有linux没有的功能. Ashmem模块在内核层面上实现,在运行时库和应用程序框架层提供了访问接口.在运行时库层提供的是C++接口,在应 ...
- 搜索入门之dfs--经典的迷宫问题解析
今天来谈一下dfs的入门,以前看到的dfs入门,那真的是入门吗,都是把dfs的实现步骤往那一贴,看完是知道dfs的步骤了,但是对于代码实现还是没有概念.今天准备写点自己的心得,真的是字面意思--入门. ...
- 【转载】pygame的斜线运动
pygame是用来写2D游戏的. 实现斜线运动,无非是在x和y两个方向上分运动的合成.x方向上的运动,当撞到边界的时候取相反速度就好了. 这里是用网球王子中的图片,以及一个网球实现,效果截图: 注意看 ...
- Web前端开发最佳实践系列文章汇总
Web前端开发最佳实践(1):前端开发概述 Web前端开发最佳实践(2):前端代码重构 Web前端开发最佳实践(3):前端代码和资源的压缩与合并 Web前端开发最佳实践(4):在页面中添加必要的met ...
- Django实战(4):scaffold生成物分析
在上一节用一个插件生成了类似rails的scaffold,其实无非就是URLconf+MTV.让我们看看具体都生成了哪些东西. 首先是“入口”的定义即URLconf,打开urls.py: from d ...