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 ...
随机推荐
- 学习java 7.16
学习内容: 线程安全的类 Lock锁 生产者消费者模式 Object类的等待唤醒方法 明天内容: 网络编程 通信程序 遇到问题: 无
- A Child's History of England.39
He had become Chancellor, when the King thought of making him Archbishop. He was clever, gay, well e ...
- Spark基础:(二)Spark RDD编程
1.RDD基础 Spark中的RDD就是一个不可变的分布式对象集合.每个RDD都被分为多个分区,这些分区运行在分区的不同节点上. 用户可以通过两种方式创建RDD: (1)读取外部数据集====> ...
- flink01--------1.flink简介 2.flink安装 3. flink提交任务的2种方式 4. 4flink的快速入门 5.source 6 常用算子(keyBy,max/min,maxBy/minBy,connect,union,split+select)
1. flink简介 1.1 什么是flink Apache Flink是一个分布式大数据处理引擎,可以对有限数据流(如离线数据)和无限流数据及逆行有状态计算(不太懂).可以部署在各种集群环境,对各种 ...
- 源码分析-Consumer
消息消费概述 消息消费以组的模式开展,一个消费组内可以包含多个消费者,每一个消费者组可订阅多个主题,消费组之间有集群模式和广播模式两种消费模式. 集群模式,主题下的同一条消息只允许被其中一个消费者消费 ...
- 生产环境高可用centos7 安装配置RocketMQ-双主双从-同步双写(2m-2s-sync)
添加hosts信息[四台机器] vim /etc/hosts 192.168.119.130 rocketmq-nameserver1 192.168.119.130 rocketmq-master1 ...
- Linux:-e、-d、-f、-L、-r、-w、-x、-s、-h、
-e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fil ...
- java foreach循环抛出异常java.util.ConcurrentModificationException
代码如下: for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { if (Integer.parseInt(i ...
- SpringIOC原理
IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...
- 【Linux】【Commands】文本查看类
分屏查看命令:more和less more命令: more FILE 特点:翻屏至文件尾部后自动退出: less命令: less FILE head命令: 查看文件的前n行: head [option ...