|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../count-triplets-that-can-form-two-arrays-of-equal-xor "Count Triplets That Can Form Two Arrays of Equal XOR") |
| 9 | + |
| 10 | +[Next >](../number-of-ways-of-cutting-a-pizza "Number of Ways of Cutting a Pizza") |
| 11 | + |
| 12 | +## [1443. Minimum Time to Collect All Apples in a Tree (Medium)](https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree "收集树上所有苹果的最少时间") |
| 13 | + |
| 14 | +<p>Given an undirected tree consisting of <code>n</code> vertices numbered from 0 to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend in order to collect all apples in the tree starting at <strong>vertex 0</strong> and coming back to this vertex.</em></p> |
| 15 | + |
| 16 | +<p>The edges of the undirected tree are given in the array <code>edges</code>, where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> means that exists an edge connecting the vertices <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>. Additionally, there is a boolean array <code>hasApple</code>, where <code>hasApple[i] = true</code> means that vertex <code>i</code> has an apple, otherwise, it does not have any apple.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong>Example 1:</strong></p> |
| 20 | + |
| 21 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/23/min_time_collect_apple_1.png" style="width: 300px; height: 212px;" /></strong></p> |
| 22 | + |
| 23 | +<pre> |
| 24 | +<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false] |
| 25 | +<strong>Output:</strong> 8 |
| 26 | +<strong>Explanation:</strong> 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. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p><strong>Example 2:</strong></p> |
| 30 | + |
| 31 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/23/min_time_collect_apple_2.png" style="width: 300px; height: 212px;" /></strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false] |
| 35 | +<strong>Output:</strong> 6 |
| 36 | +<strong>Explanation:</strong> 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. |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p><strong>Example 3:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false] |
| 43 | +<strong>Output:</strong> 0 |
| 44 | +</pre> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | +<p><strong>Constraints:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li><code>1 <= n <= 10^5</code></li> |
| 51 | + <li><code>edges.length == n-1</code></li> |
| 52 | + <li><code>edges[i].length == 2</code></li> |
| 53 | + <li><code>0 <= from<sub>i</sub>, to<sub>i</sub> <= n-1</code></li> |
| 54 | + <li><code>from<sub>i</sub> < to<sub>i</sub></code></li> |
| 55 | + <li><code>hasApple.length == n</code></li> |
| 56 | +</ul> |
| 57 | + |
| 58 | +### Related Topics |
| 59 | + [[Tree](../../tag/tree/README.md)] |
| 60 | + [[Depth-first Search](../../tag/depth-first-search/README.md)] |
| 61 | + |
| 62 | +### Hints |
| 63 | +<details> |
| 64 | +<summary>Hint 1</summary> |
| 65 | +Note that if a node u contains an apple then all edges in the path from the root to the node u have to be used forward and backward (2 times). |
| 66 | +</details> |
| 67 | + |
| 68 | +<details> |
| 69 | +<summary>Hint 2</summary> |
| 70 | +Therefore use a depth-first search (DFS) to check if an edge will be used or not. |
| 71 | +</details> |
0 commit comments