2378. Choose Edges to Maximize Score in a Tree π
DifficultyMedium
Description
You are given a weighted tree consisting of n nodes numbered from 0 to n - 1.
The tree is rooted at node 0 and represented with a 2D array edges of size n where edges[i] = [pari, weighti] indicates that node pari is the parent of node i, and the edge between them has a weight equal to weighti. Since the root does not have a parent, you have edges[0] = [-1, -1].
Choose some edges from the tree such that no two chosen edges are adjacent and the sum of the weights of the chosen edges is maximized.
Return the maximum sum of the chosen edges.
Note:
- You are allowed to not choose any edges in the tree, the sum of weights in this case will be
0. - Two edges
Edge1andEdge2in the tree are adjacent if they have a common node.- In other words, they are adjacent if
Edge1connects nodesaandbandEdge2connects nodesbandc.
- In other words, they are adjacent if
Example 1:
Input: edges = [[-1,-1],[0,5],[0,10],[2,6],[2,4]] Output: 11 Explanation: The above diagram shows the edges that we have to choose colored in red. The total score is 5 + 6 = 11. It can be shown that no better score can be obtained.
Example 2:
Input: edges = [[-1,-1],[0,5],[0,-6],[0,7]] Output: 7 Explanation: We choose the edge with weight 7. Note that we cannot choose more than one edge because all edges are adjacent to each other.
Constraints:
n == edges.length1 <= n <= 105edges[i].length == 2par0 == weight0 == -10 <= pari <= n - 1for alli >= 1.pari != i-106 <= weighti <= 106for alli >= 1.edgesrepresents a valid tree.
Solutions
Solution 1: Tree DP
Thinking
Choose a matching of tree edges with maximum weight. A choice only affects the two endpoints, so a tree DP is enough.
\(\textit{dfs}(i)\) returns the best weight when the edge to the parent is taken, and when it is not. The former sums childrenβs βnot takenβ values; the latter may switch at most one child to βtakenβ and add that edge weight.
We design a function \(\textit{dfs}(i)\), which represents the maximum sum of the weights of selected edges in the subtree rooted at node \(i\), such that no two selected edges are adjacent. This function returns two values \((a, b)\). The first value \(a\) represents the sum of the weights of selected edges when the edge between the current node \(i\) and its parent node is selected. The second value \(b\) represents the sum of the weights of selected edges when the edge between the current node \(i\) and its parent node is not selected.
We can observe the following for the current node \(i\):
- If the edge between \(i\) and its parent node is selected, then none of the edges between \(i\) and its child nodes can be selected. In this case, the value of \(a\) for the current node is the sum of the \(b\) values of all its child nodes.
- If the edge between \(i\) and its parent node is not selected, then we can select at most one edge between \(i\) and its child nodes. In this case, the value of \(b\) for the current node is the sum of the \(a\) values of the selected child nodes and the \(b\) values of the unselected child nodes, plus the weight of the edge between \(i\) and the selected child node.
We call the function \(\textit{dfs}(0)\), and the second value returned is the answer, which is the sum of the weights of selected edges when the edge between the root node and its parent node is not selected.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of nodes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Solution 2: Tree DP + Explicit Stack
Thinking
Choose a set of non-adjacent tree edges with maximum weight. With \(n \le 10^5\), deciding each edge by recursion from the root is too deep: a chain makes the call depth \(n\).
Each node needs two values: the best weight \(a\) when the edge to its parent is taken, and the best weight \(b\) when that edge is not taken. Taking the parent edge forbids every edge to a child, so \(a\) is the sum of the children's \(b\) values. Leaving it free allows at most one child edge, and the gain of switching that child is its \(a\) minus its \(b\) plus the edge weight.
An explicit stack of \((\textit{node}, \textit{state})\) runs the postorder. On entry we push the exit marker and then the children, and on exit we store \(a\) and \(b\). The root has no parent edge, so the answer is the root's \(b\).
For each node \(i\) we compute two values \((a, b)\). The value \(a\) is the maximum weight of selected edges in the subtree when the edge between \(i\) and its parent is selected. The value \(b\) is that weight when the edge is not selected. An explicit stack walks from the root in postorder and writes the pair when the node is left.
For the current node \(i\):
- If the edge between \(i\) and its parent is selected, then none of the edges between \(i\) and its children can be selected. In this case \(a\) is the sum of the \(b\) values of all children.
- If that edge is not selected, then at most one edge from \(i\) to a child can be selected. In this case \(b\) is the selected child's \(a\), plus the \(b\) values of the other children, plus the weight of the selected edge. A negative gain leaves the edge unselected.
The root has no parent edge, so the answer is the root's \(b\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of nodes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |

