Minimum Cost to Make Red Nodes Connected Problem
Description
LeetCode Problem 4108.
You are given an integer n representing a tree with n nodes, where the nodes are numbered from 0 to n - 1 and the tree is rooted at node 0. The tree is represented by a 2D array edges of length n - 1, where edges[i] = [u_i, v_i] indicates an undirected edge between nodes u_i and v_i.
You are also given a string colors of length n, where colors[i] represents the color of node i: 'R' if it is Red, or 'B' if it is Blue.
You may flip the color of any node: from 'R' to 'B', or from 'B' to 'R'. Flipping node u incurs a cost equal to the size of the subtree of node u.
Your goal is to perform flips so the Red nodes form a single connected component in the tree. Blue nodes may be disconnected. A configuration with no Red nodes is also considered valid.
Return the minimum total cost required to achieve this.
Example 1:
Input: n = 3, colors = "RBR", edges = [[0,1],[1,2]]
Output: 1
Explanation:

- There are two Red nodes (0 and 2) that are not connected to each other.
- We can flip node 2 from 'R' (Red) to 'B' (Blue). The subtree size of node 2 is 1, so the cost is 1.
- After this flip only node 0 remains Red, forming exactly one connected Red component.
Example 2:
Input: n = 3, colors = "RBR", edges = [[0,1],[0,2]]
Output: 0
Explanation:

- There are two Red nodes (0 and 2) that are already connected to each other.
- No flips are needed since all Red nodes already form a single connected group. Thus, the minimum total cost is 0.
Example 3:
Input: n = 4, colors = "BRRR", edges = [[0,1],[0,2],[0,3]]
Output: 2
Explanation:

- There are three Red nodes (1, 2 and 3), which are not connected to each other.
- All three Red nodes are leaves and are disconnected from one another.
- To obtain a single Red group, we keep one Red leaf and flip the other two to Blue.
- Each leaf subtree size is 1, so flipping any two costs 2.
Example 4:
Input: n = 3, colors = "BBB", edges = [[0,1],[1,2]]
Output: 0
Explanation:

- All nodes are Blue, so there are no Red nodes.
- An empty Red group is considered a single connected group, so this state is already valid. Thus, the cost is 0.
Constraints:
1 <= n <= 10^5n == colors.lengthcolors[i]is either'R'or'B'.edges.length == n - 1edges[i] = [u_i, v_i]0 <= u_i, v_i <= n - 1- The input is generated such that
edgesrepresents a valid tree.
Solution
Approach: Tree DP on the Top of the Red Component
In the final coloring, the red nodes form a connected piece that has a topmost node t (or there are no red nodes at all). Compute these values bottom-up, with size[v] the subtree size:
red[v]: the cost of making every red node in the subtree ofvblue, which is the sum ofsize[u]over red nodesuin that subtree.keep[v]: the cheapest cost inside the subtree ofvwhenvends up red and the red nodes there form one piece containingv. It issize[v]ifvstarts blue (we must flip it), plus, for each childc, the cheaper ofkeep[c](the piece continues intoc) andred[c](nothing red remains underc).
If the top is t, everything outside the subtree of t must be blue, which costs red[0] - red[t]. So the answer is the smaller of red[0] (no red nodes) and keep[t] + red[0] - red[t] over all t.
Complexity: O(n) time and O(n) space.
class Solution:
def minCost(self, n: int, colors: str, edges: List[List[int]]) -> int:
adj = [[] for _ in range(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
# BFS order from the root, so that children come after their parent
parent = [-1] * n
order = [0]
for v in order:
for w in adj[v]:
if w != parent[v]:
parent[w] = v
order.append(w)
size = [1] * n
# red[v]: cost of turning every red node in v's subtree blue
red = [0] * n
# keep[v]: cheapest cost inside v's subtree when v stays red and the red nodes there are connected to v
keep = [0] * n
for v in reversed(order):
if colors[v] == "R":
red[v] += size[v]
else:
keep[v] += size[v]
p = parent[v]
if p >= 0:
size[p] += size[v]
red[p] += red[v]
keep[p] += min(keep[v], red[v])
# the red component hangs from some top node t; red nodes outside t's subtree must all be flipped
ans = red[0]
for t in range(n):
ans = min(ans, keep[t] + red[0] - red[t])
return ans