|
| 1 | +/** |
| 2 | + * 2322. Minimum Score After Removals on a Tree |
| 3 | + * https://leetcode.com/problems/minimum-score-after-removals-on-a-tree/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. |
| 7 | + * |
| 8 | + * You are given a 0-indexed integer array nums of length n where nums[i] represents the value |
| 9 | + * of the ith node. You are also given a 2D integer array edges of length n - 1 where |
| 10 | + * edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. |
| 11 | + * |
| 12 | + * Remove two distinct edges of the tree to form three connected components. For a pair of |
| 13 | + * removed edges, the following steps are defined: |
| 14 | + * 1. Get the XOR of all the values of the nodes for each of the three components respectively. |
| 15 | + * 2. The difference between the largest XOR value and the smallest XOR value is the score of |
| 16 | + * the pair. |
| 17 | + * 3. For example, say the three components have the node values: [4,5,7], [1,9], and [3,3,3]. |
| 18 | + * The three XOR values are 4 ^ 5 ^ 7 = 6, 1 ^ 9 = 8, and 3 ^ 3 ^ 3 = 3. The largest XOR |
| 19 | + * value is 8 and the smallest XOR value is 3. The score is then 8 - 3 = 5. |
| 20 | + * |
| 21 | + * Return the minimum score of any possible pair of edge removals on the given tree. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | +* @param {number[]} nums |
| 26 | +* @param {number[][]} edges |
| 27 | +* @return {number} |
| 28 | +*/ |
| 29 | +var minimumScore = function(nums, edges) { |
| 30 | + const n = nums.length; |
| 31 | + const graph = Array.from({ length: n }, () => []); |
| 32 | + |
| 33 | + for (const [a, b] of edges) { |
| 34 | + graph[a].push(b); |
| 35 | + graph[b].push(a); |
| 36 | + } |
| 37 | + |
| 38 | + const totalXor = nums.reduce((acc, num) => acc ^ num, 0); |
| 39 | + |
| 40 | + const subtreeXor = new Array(n).fill(0); |
| 41 | + |
| 42 | + const tin = new Array(n); |
| 43 | + const tout = new Array(n); |
| 44 | + let timer = 0; |
| 45 | + |
| 46 | + function dfs(node, parent) { |
| 47 | + tin[node] = timer++; |
| 48 | + subtreeXor[node] = nums[node]; |
| 49 | + |
| 50 | + for (const neighbor of graph[node]) { |
| 51 | + if (neighbor !== parent) { |
| 52 | + dfs(neighbor, node); |
| 53 | + subtreeXor[node] ^= subtreeXor[neighbor]; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + tout[node] = timer++; |
| 58 | + } |
| 59 | + |
| 60 | + dfs(0, -1); |
| 61 | + |
| 62 | + function isAncestor(a, b) { |
| 63 | + return tin[a] <= tin[b] && tout[a] >= tout[b]; |
| 64 | + } |
| 65 | + |
| 66 | + let result = Infinity; |
| 67 | + for (let i = 0; i < n - 1; i++) { |
| 68 | + for (let j = i + 1; j < n - 1; j++) { |
| 69 | + const edge1 = edges[i]; |
| 70 | + const edge2 = edges[j]; |
| 71 | + |
| 72 | + let node1; |
| 73 | + if (isAncestor(edge1[0], edge1[1])) { |
| 74 | + node1 = edge1[1]; |
| 75 | + } else { |
| 76 | + node1 = edge1[0]; |
| 77 | + } |
| 78 | + const subtree1 = subtreeXor[node1]; |
| 79 | + |
| 80 | + let node2; |
| 81 | + if (isAncestor(edge2[0], edge2[1])) { |
| 82 | + node2 = edge2[1]; |
| 83 | + } else { |
| 84 | + node2 = edge2[0]; |
| 85 | + } |
| 86 | + const subtree2 = subtreeXor[node2]; |
| 87 | + |
| 88 | + let component1; |
| 89 | + let component2; |
| 90 | + let component3; |
| 91 | + if (isAncestor(node1, node2)) { |
| 92 | + component1 = subtree2; |
| 93 | + component2 = subtree1 ^ component1; |
| 94 | + component3 = totalXor ^ subtree1; |
| 95 | + } else if (isAncestor(node2, node1)) { |
| 96 | + component1 = subtree1; |
| 97 | + component2 = subtree2 ^ component1; |
| 98 | + component3 = totalXor ^ subtree2; |
| 99 | + } else { |
| 100 | + component1 = subtree1; |
| 101 | + component2 = subtree2; |
| 102 | + component3 = totalXor ^ component1 ^ component2; |
| 103 | + } |
| 104 | + |
| 105 | + const maxXor = Math.max(component1, component2, component3); |
| 106 | + const minXor = Math.min(component1, component2, component3); |
| 107 | + result = Math.min(result, maxXor - minXor); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return result; |
| 112 | +}; |
0 commit comments