Codeforces Round #358 (Div. 2) C. Alyona and the Tree
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:
题意 :给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的更多相关文章
- 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(树的DFS+逆向思维)
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 树上二分+前缀和思想
题目链接: 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 ...
- Codeforces Round #358 (Div. 2) E. Alyona and Triangles 随机化
E. Alyona and Triangles 题目连接: http://codeforces.com/contest/682/problem/E Description You are given ...
- 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 ...
随机推荐
- C# js获取buttonid
var id= document.getElementById('<%=控件的ID.ClientID %>');
- addict, address, adequate.四级
addict addiction – a biopsychosocial [生物社会心理学的 bio-psycho-social] disorder characterized by persiste ...
- day03 Django目录结构与reques对象方法
day03 Django目录结构与reques对象方法 今日内容概要 django主要目录结构 创建app注意事项(重点) djago小白必会三板斧 静态文件配置(登录功能) requeste对象方法 ...
- flink-----实时项目---day04-------1. 案例:统计点击、参与某个活动的人数和次数 2. 活动指标多维度统计(自定义redisSink)
1. 案例 用户ID,活动ID,时间,事件类型,省份 u001,A1,2019-09-02 10:10:11,1,北京市 u001,A1,2019-09-02 14:10:11,1,北京市 u001, ...
- Kafka(一)【概述、入门、架构原理】
目录 一.Kafka概述 1.1 定义 二.Kafka快速入门 2.1 安装部署 2.2 配置文件解析 2.3Kafka群起脚本 2.4 topic(增删改查) 2.5 生产和消费者命令行操作 三.K ...
- CSS系列,三栏布局的四种方法
三栏布局.两栏布局都是我们在平时项目里经常使用的,今天我们来玩一下三栏布局的四种写法,以及它的使用场景. 所谓三栏布局就是指页面分为左中右三部分然后对中间一部分做自适应的一种布局方式. 1.绝对定位法 ...
- Linux 易错小结
修改文件夹(递归修改)权限 chmod -R 777 /html Linux查看进程的4种方法 第一种: ps aux ps命令用于报告当前系统的进程状态.可以搭配kill指令随时中断.删除不必要的程 ...
- 赋能开发:捷码携手达内教育打造IT职业教育新生态
近日,达内教育与远眺科技签约联合培养的第一批低代码开发方向的高职学生,在杭州未来科技城捷码总部顺利毕业,首期合格学员总数超过30名.随着这些接受了"捷码"低代码平台全程" ...
- 7、Redis五大数据类型---集合(Set)
一.集合(Set)简介 Set是string类型的无序集合.集合成员是唯一的,这就意味着集合中不能出现重复的数据. Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1). ...
- redis实例cpu占用率过高问题优化
目录 一.简介 一.简介 前情提要: 最近接了大数据项目的postgresql运维,刚接过来他们的报表系统就出现高峰期访问不了的问题,报表涉及实时数据和离线数据,离线读pg,实时读redis.然后自然 ...