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^5
  • edges.length == n-1
  • edges[i].length == 2
  • 0 <= fromi, toi <= n-1
  • fromi < toi
  • hasApple.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 };

参考:

  https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/discuss/623673/C%2B%2B-Java-Detailed-explanation-with-a-Picture-for-visualization

1443. Minimum Time to Collect All Apples in a Tree的更多相关文章

  1. Range Minimum Query and Lowest Common Ancestor

    作者:danielp 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAnc ...

  2. hdu多校第4场 B Harvest of Apples(莫队)

    Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  3. 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 ...

  4. hdu 6406 Taotao Picks Apples 线段树 单点更新

    Taotao Picks Apples Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  5. [乱搞]hdu 6406 Taotao picks apples 笛卡尔树+倍增

    题目链接 Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n app ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Harvest of Apples

    问题 B: Harvest of Apples 时间限制: 1 Sec  内存限制: 128 MB提交: 18  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...

随机推荐

  1. 双重检验锁模式为什么要使用volatile?

    并发编程情况下有三个要点:操作的原子性.可见性.有序性. volatile保证了可见性和有序性,但是并不能保证原子性. 首先看一下DCL(双重检验锁)的实现: public class Singlet ...

  2. Linux系统编程【4】——文件系统

    pwd命令的作用 Linux的文件系统比较庞大,所以笔者从pwd这一命令入手,在实现的过程中加深对文件系统的了解. 输入:man pwd 从指导文档中可以看到,pwd命令的作用是显示出当前所处位置,以 ...

  3. 死磕hyperledger fabric源码|Order节点概述

    死磕hyperledger fabric源码|Order节点概述 文章及代码:https://github.com/blockchainGuide/ 分支:v1.1.0 前言及源码目录 Orderer ...

  4. void指针及指针的多次赋值的理解

    1.void指针的类型转换 int A::functionCommamd(const DWORD _from,const DWORD _to,const DWORD Event_type,void * ...

  5. Course2.1 Graph Paper Programming

    Overview 通过日常生活中的活动来体验程序算法,目标时能够将现实世界的场景与程序场景关联起来. Objective 抓住将现实世界问题转换为程序的难点: 你认为非常明确的指令在计算机看来可能还是 ...

  6. 手把手教你SpringBoot2整合Redis

    此文仅为初学java的同学学习,大佬请勿喷,文末我会附上完整代码包供大家参考 redis的搭建教程此处略过,大家自行百度,本文的教程开始: 一.先在pom.xml中添加相关依赖 <!--redi ...

  7. 10万级etl调度软件Taskctl-web版免费授权及产品功能特性

    转: 10万级etl调度软件Taskctl-web版免费授权及产品功能特性 初识Taskctl-Web版 Taskctl Free应用版原型是在原有商用版Taskctl 6.0衍生扩展开发出的专门为批 ...

  8. 从客流统计到营销赋能,Re-ID加速实体商业数字化转型 | 爱分析洞见

    2020年中国实体商业受到突发疫情的重大影响.以危机为契机,实体商业加速数字化转型,利用创新应用服务自身业务.在此阶段,基于Re-ID(Person Re-identification,即行人再识别) ...

  9. 记录实践PC端微信防撤回实现过程(基于3.1.0.67版本)

    利用OD实现对PC端微信防撤回功能的实现 文章最后有一键补丁工具哦~ 准备工具 1.OD 2.PC微信客户端(3.1.0.67) 过程 1.运行微信客户端,不需要登录 2.运行OD,左上角选择附加进程 ...

  10. 写个锤子JS!它应该是你最后的选择

    本文翻译自:https://dev.to/olpeh/javascript-should-be-your-last-resort-5dje 在进行现代化Web前端开发时,使用着自己最爱的框架,有时候可 ...