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 ...
随机推荐
- C语言柔性数组和动态数组
[前言]经常看到C语言里的两个数组,总结一下. 一.柔性数组 参考:https://www.cnblogs.com/veis/p/7073076.html #include<stdio.h> ...
- CSS:CSS基础
和 HTML 类似,CSS 也不是真正的编程语言,甚至不是标记语言.它是一门样式表语言,这也就是说人们可以用它来选择性地为 HTML 元素添加样式. CSS规则集 选择器(Selector):元素的名 ...
- HTML:HTML基础
HTML不是一门编程语言,而是一种用于定义内容结构的标记语言.HTML由一系列元素(elements)组成,这些元素可以用来包围不同部分的内容,使其以某种方式呈现或工作.一对标签可以为一段文字或者一张 ...
- go mod管理 init 和 包导入的关系
你创建了一个文件的名字为:lisi001 如果你初始化项目名字为lisi, go mod init lisi 那么你导包的时候就得也用lisi import ( "lisi/path&quo ...
- DES加密详解
目录 1 根据输入的秘钥得到16个子秘钥 1.1 大致流程 1.2 利用PC-1从K_0中挑出K_1 1.3 利用PC-2从K_1中挑出16个子秘钥 2 利用16个子秘钥对明文进行加密 2.1 大致流 ...
- Java并发编程基础三板斧之Semaphore
引言 最近可以进行个税申报了,还没有申报的同学可以赶紧去试试哦.不过我反正是从上午到下午一直都没有成功的进行申报,一进行申报 就返回"当前访问人数过多,请稍后再试".为什么有些人就 ...
- [个人总结]利用grad-cam实现人民币分类
# -*- coding:utf-8 -*- import os import numpy as np import torch import cv2 import torch.nn as nn fr ...
- 【知识点】 C++寄存器优化
作者:李春港 出处:https://www.cnblogs.com/lcgbk/p/14502076.html 目录 一.前言 二.代码实例 三.volatile作用 一.前言 在c++中什么情况下, ...
- 安卓Media相关类测试demo
最近在研究安卓系统给app开发者提供的标准Media相关的工具类,本人做了一些demo来测试这些工具的使用方法. 本demo包含若干apk源码,需要说明以下几点: 1. 构建方式 Makefile使用 ...
- flutter简易教程
跟Java等很多语言不同的是,Dart没有public protected private等关键字,如果某个变量以下划线 _ 开头,代表这个变量在库中是私有的.Dart中变量可以以字母或下划线开头,后 ...