Maximum Subgraph Score in a Tree Problem
Description
LeetCode Problem 3772.
You are given an undirected tree with n nodes, numbered from 0 to n - 1. It is represented by a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.
You are also given an integer array good of length n, where good[i] is 1 if the i^th node is good, and 0 if it is bad.
Define the score of a subgraph as the number of good nodes minus the number of bad nodes in that subgraph.
For each node i, find the maximum possible score among all connected subgraphs that contain node i.
Return an array of n integers where the i^th element is the maximum score for node i.
A subgraph is a graph whose vertices and edges are subsets of the original graph.
A connected subgraph is a subgraph in which every pair of its vertices is reachable from one another using only its edges.
Example 1:

Input: n = 3, edges = [[0,1],[1,2]], good = [1,0,1]
Output: [1,1,1]
Explanation:
- Green nodes are good and red nodes are bad.
- For each node, the best connected subgraph containing it is the whole tree, which has 2 good nodes and 1 bad node, resulting in a score of 1.
- Other connected subgraphs containing a node may have the same score.
Example 2:

Input: n = 5, edges = [[1,0],[1,2],[1,3],[3,4]], good = [0,1,0,1,1]
Output: [2,3,2,3,3]
Explanation:
- Node 0: The best connected subgraph consists of nodes 0, 1, 3, 4, which has 3 good nodes and 1 bad node, resulting in a score of 3 - 1 = 2.
- Nodes 1, 3, and 4: The best connected subgraph consists of nodes 1, 3, 4, which has 3 good nodes, resulting in a score of 3.
- Node 2: The best connected subgraph consists of nodes 1, 2, 3, 4, which has 3 good nodes and 1 bad node, resulting in a score of 3 - 1 = 2.
Example 3:

Input: n = 2, edges = [[0,1]], good = [0,0]
Output: [-1,-1]
Explanation:
For each node, including the other node only adds another bad node, so the best score for both nodes is -1.
Constraints:
2 <= n <= 10^5edges.length == n - 1edges[i] = [a_i, b_i]0 <= a_i, b_i < ngood.length == n0 <= good[i] <= 1- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Tree DP + Rerooting
Give each node a value of +1 if it is good and -1 if it is bad. Root the tree at node 0 and compute down[u]: the best score of a connected subgraph that contains u and stays inside the subtree of u. It equals the value of u plus max(0, down[c]) over its children c, because a child's part is worth taking only when it is positive.
For the root the answer is down[0]. For a child u of p, the best subgraph through u may also extend upwards. What p can offer is ans[p] - max(0, down[u]), that is its best subgraph without the branch of u, so ans[u] = down[u] + max(0, ans[p] - max(0, down[u])). Process the nodes in BFS order for the first pass in reverse and for the second pass forward, which avoids deep recursion.
Complexity: time O(n), space O(n), where n is the number of nodes.
class Solution:
def maxSubgraphScore(self, n: int, edges: List[List[int]], good: List[int]) -> List[int]:
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
# BFS order from root 0, so parents come before children
parent = [-1] * n
order = [0]
for u in order:
for v in g[u]:
if v != parent[u]:
parent[v] = u
order.append(v)
# down[u]: best score of a connected subgraph rooted at u inside u's subtree
down = [1 if good[u] else -1 for u in range(n)]
for u in reversed(order):
if parent[u] >= 0:
down[parent[u]] += max(0, down[u])
ans = [0] * n
ans[0] = down[0]
for u in order[1:]:
p = parent[u]
# best subgraph rooted at p that does not use u's subtree
without = ans[p] - max(0, down[u])
ans[u] = down[u] + max(0, without)
return ans