mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
53 lines
3.1 KiB
HTML
53 lines
3.1 KiB
HTML
|
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||
|
|
||
|
<p>You are also given a <strong>0-indexed</strong> integer array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> associated with the <code>i<sup>th</sup></code> node.</p>
|
||
|
|
||
|
<p>You start with a score of <code>0</code>. In one operation, you can:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>Pick any node <code>i</code>.</li>
|
||
|
<li>Add <code>values[i]</code> to your score.</li>
|
||
|
<li>Set <code>values[i]</code> to <code>0</code>.</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>A tree is <strong>healthy</strong> if the sum of values on the path from the root to any leaf node is different than zero.</p>
|
||
|
|
||
|
<p>Return <em>the <strong>maximum score</strong> you can obtain after performing these operations on the tree any number of times so that it remains <strong>healthy</strong>.</em></p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong class="example">Example 1:</strong></p>
|
||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-13-1.png" style="width: 515px; height: 443px;" />
|
||
|
<pre>
|
||
|
<strong>Input:</strong> edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
|
||
|
<strong>Output:</strong> 11
|
||
|
<strong>Explanation:</strong> We can choose nodes 1, 2, 3, 4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1] + values[2] + values[3] + values[4] + values[5] = 11.
|
||
|
It can be shown that 11 is the maximum score obtainable after any number of operations on the tree.
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 2:</strong></p>
|
||
|
<img alt="" src="https://assets.leetcode.com/uploads/2023/10/11/graph-14-2.png" style="width: 522px; height: 245px;" />
|
||
|
<pre>
|
||
|
<strong>Input:</strong> edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
|
||
|
<strong>Output:</strong> 40
|
||
|
<strong>Explanation:</strong> We can choose nodes 0, 2, 3, and 4.
|
||
|
- The sum of values on the path from 0 to 4 is equal to 10.
|
||
|
- The sum of values on the path from 0 to 3 is equal to 10.
|
||
|
- The sum of values on the path from 0 to 5 is equal to 3.
|
||
|
- The sum of values on the path from 0 to 6 is equal to 5.
|
||
|
Therefore, the tree is healthy and the score is values[0] + values[2] + values[3] + values[4] = 40.
|
||
|
It can be shown that 40 is the maximum score obtainable after any number of operations on the tree.
|
||
|
</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>2 <= n <= 2 * 10<sup>4</sup></code></li>
|
||
|
<li><code>edges.length == n - 1</code></li>
|
||
|
<li><code>edges[i].length == 2</code></li>
|
||
|
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||
|
<li><code>values.length == n</code></li>
|
||
|
<li><code>1 <= values[i] <= 10<sup>9</sup></code></li>
|
||
|
<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
|
||
|
</ul>
|