Class Solution
-
- All Implemented Interfaces:
public final class Solution2440 - Create Components With Same Value.
Hard
There is an undirected tree with
nnodes labeled from0ton - 1.You are given a 0-indexed integer array
numsof lengthnwherenums[i]represents the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer arrayedgesof lengthn - 1where <code>edgesi = 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.You are allowed to delete some edges, splitting the tree into multiple connected components. Let the value of a component be the sum of all
nums[i]for which nodeiis in the component.Return the maximum number of edges you can delete, such that every connected component in the tree has the same value.
Example 1:
Input: nums = 6,2,2,2,6, edges = \[\[0,1],1,2,1,3,3,4]
Output: 2
Explanation: The above figure shows how we can delete the edges 0,1 and 3,4. The created components are nodes 0, 1,2,3 and 4. The sum of the values in each component equals 6. It can be proven that no better deletion exists, so the answer is 2.
Example 2:
Input: nums = 2, edges = []
Output: 0
Explanation: There are no edges to be deleted.
Constraints:
<code>1 <= n <= 2 * 10<sup>4</sup></code>
nums.length == n1 <= nums[i] <= 50edges.length == n - 1edges[i].length == 20 <= edges[i][0], edges[i][1] <= n - 1edgesrepresents a valid tree.