3068. Find the Maximum Sum of Node Values Hard

@problem@discussion
#Array#Dynamic Programming#Greedy#Bit Manipulation#Tree#Sorting



1/**
2 * [3068] Find the Maximum Sum of Node Values
3 *
4 * There exists an undirected tree with n nodes numbered 0 to n - 1. You are given a 0-indexed 2D integer array edges of length n - 1, where edges[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the tree. You are also given a positive integer k, and a 0-indexed array of non-negative integers nums of length n, where nums[i] represents the value of the node numbered i.
5 * Alice wants the sum of values of tree nodes to be maximum, for which Alice can perform the following operation any number of times (including zero) on the tree:
6 * 
7 * 	Choose any edge [u, v] connecting the nodes u and v, and update their values as follows:
8 * 	
9 * 		nums[u] = nums[u] XOR k
10 * 		nums[v] = nums[v] XOR k
11 * 	
12 * 	
13 * 
14 * Return the maximum possible sum of the values Alice can achieve by performing the operation any number of times.
15 *  
16 * <strong class="example">Example 1:
17 * <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" />
18 * Input: nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
19 * Output: 6
20 * Explanation: Alice can achieve the maximum sum of 6 using a single operation:
21 * - Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
22 * The total sum of values is 2 + 2 + 2 = 6.
23 * It can be shown that 6 is the maximum achievable sum of values.
24 * 
25 * <strong class="example">Example 2:
26 * <img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" />
27 * Input: nums = [2,3], k = 7, edges = [[0,1]]
28 * Output: 9
29 * Explanation: Alice can achieve the maximum sum of 9 using a single operation:
30 * - Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4].
31 * The total sum of values is 5 + 4 = 9.
32 * It can be shown that 9 is the maximum achievable sum of values.
33 * 
34 * <strong class="example">Example 3:
35 * <img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" />
36 * Input: nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
37 * Output: 42
38 * Explanation: The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
39 * 
40 *  
41 * Constraints:
42 * 
43 * 	2 <= n == nums.length <= 2 * 10^4
44 * 	1 <= k <= 10^9
45 * 	0 <= nums[i] <= 10^9
46 * 	edges.length == n - 1
47 * 	edges[i].length == 2
48 * 	0 <= edges[i][0], edges[i][1] <= n - 1
49 * 	The input is generated such that edges represent a valid tree.
50 * 
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/find-the-maximum-sum-of-node-values/
55// discuss: https://leetcode.com/problems/find-the-maximum-sum-of-node-values/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60    pub fn maximum_value_sum(nums: Vec<i32>, k: i32, edges: Vec<Vec<i32>>) -> i64 {
61        
62    }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_3068() {
73    }
74}
75


Back
© 2025 bowen.ge All Rights Reserved.