1443. Minimum Time to Collect All Apples in a Tree
Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend in order to collect all apples in the tree starting at vertex 0 and coming back to this vertex.
The edges of the undirected tree are given in the array edges, where edges[i] = [fromi, toi] means that exists an edge connecting the vertices fromi and toi. Additionally, there is a boolean array hasApple, where hasApple[i] = true means that vertex i has an apple, otherwise, it does not have any apple.
Example 1:

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]
Output: 8
Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.
Example 2:

Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]
Output: 6
Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.
Example 3:
Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]
Output: 0
Constraints:
1 <= n <= 10^5edges.length == n-1edges[i].length == 20 <= fromi, toi <= n-1fromi < toihasApple.length == n
题意:
给出一棵树,树中某些节点有苹果,求从根节点出发,将所有的苹果收集完,并返回根节点所需的步数。
思路:
这是一道DFS的题目,可以把树看成是一个图,然后用DFS遍历图,记录下遍历所需的步数,因为需要返回所以在寻找苹果的过程中,应该将所需的步数 * 2 。这里我们用递归的方法来遍历树,如果子树中没有苹果,则这条遍历路径的步数应该置为0. 否则将遍历到该点所需的步数累加到总步数中。
Code:
1 class Solution {
2 public:
3 vector<vector<int> > grap;
4
5 int DFS(int index, int mySteps, vector<bool>& hasApple) {
6 int childrenSteps = 0;
7 for (int i : grap[index]) {
8 childrenSteps += DFS(i, 2, hasApple);
9 }
10 if (childrenSteps == 0 && hasApple[index] == false)
11 return 0;
12 return childrenSteps + mySteps;
13
14 }
15 int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {
16 grap.resize(n + 1);
17 for (int i = 0; i < edges.size(); ++i)
18 grap[edges[i][0]].push_back(edges[i][1]);
19 return DFS(0, 0, hasApple);
20 }
21 };
参考:
1443. Minimum Time to Collect All Apples in a Tree的更多相关文章
- Range Minimum Query and Lowest Common Ancestor
作者:danielp 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAnc ...
- hdu多校第4场 B Harvest of Apples(莫队)
Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- Problem B. Harvest of Apples HDU - 6333(莫队)
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- hdu 6406 Taotao Picks Apples 线段树 单点更新
Taotao Picks Apples Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- [乱搞]hdu 6406 Taotao picks apples 笛卡尔树+倍增
题目链接 Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n app ...
- 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...
- Problem B. Harvest of Apples 莫队求组合数前缀和
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- HDU - 6333:Harvest of Apples (组合数前缀和&莫队)
There are n n apples on a tree, numbered from 1 1 to n n . Count the number of ways to pick at most ...
- Harvest of Apples
问题 B: Harvest of Apples 时间限制: 1 Sec 内存限制: 128 MB提交: 18 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...
随机推荐
- 使用gitlab构建基于docker的持续集成(三)
使用gitlab构建基于docker的持续集成(三) gitlab docker aspnetcore 持续集成 构建发布思路: aspnetcore 下的dockerfile编写 发布docker- ...
- 后端程序员之路 36、Apache Kafka
Apache Kafkahttp://kafka.apache.org/ Kafka,很容易就联想到<海边的卡夫卡>,文艺程度和Casablanca有得一拼.Kafka是一个分布式消息系统 ...
- R语言低级绘图函数画个温度计
x <- 1:2 y <- runif(2,0,100) par(mar=c(4,6,2,6)) plot(x,y,type="n",xlim=c(0.5,2.5),y ...
- pyinstaller打包exe运行失败
使用Pyinstaller来打包自己开发的软件时遇到的几个问题及解决方法.工具主要功能是数据分析,使用机器学习算法完成数据训练和预测功能.主要用到了两个学习库keras和sklearn,所以说在打包时 ...
- C++指针的算术运算 、关系运算
下面随笔是关于指针的算术运算 .关系运算. 指针类型的算术运算 指针与整数的加减运算 指针++,--运算 指针类型的算术运算 指针p加上或减去n 其意义是指针当前指向位置的前方或后方第n个数据的起始位 ...
- 653. 两数之和 IV - 输入 BST + HashSet
653. 两数之和 IV - 输入 BST 题目描述 题解分析 最简单的方法就是遍历整棵树,找出所有可能的组合,判断是否存在和为 kk 的一对节点.现在在此基础上做一些改进. 如果存在两个元素之和为 ...
- 剑指 Offer 43. 1~n 整数中 1 出现的次数 + 数位模拟 + 思维
剑指 Offer 43. 1-n 整数中 1 出现的次数 Offer_43 题目描述 题解分析 java代码 package com.walegarrett.offer; /** * @Author ...
- mysql 基本指令 1
desc 表名 --查看表属性 show create table 表名 \g; --查看代码 alter table 表名 auto_increment=20; --改自增的值 MySQL:自 ...
- List调用toString()方法后,去除两头的中括号
import org.apache.commons.lang.StringUtils; public class Test { public static void main(String[] ...
- CSS浮动布局带来的高度塌陷以及其解决办法
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...