Hints: Coloring a Red Black Tree
Answer to Hint 3: Each operation picks a uniformly random neighbor. The probability of picking a red one is $r / d$, and these trials are independent (failure leaves the node black). The count follows a geometric distribution with expected value $d / r$.
Because the geometric distribution is memoryless, there is no benefit to trying one node for a while, switching to another, and coming back. You should commit to a single node until it turns red before moving on.
Forget trees for now. Consider the problem on a path: a contiguous run of black nodes $b_1, b_2, \ldots, b_L$ with a red node at each end. Every $b_i$ has degree 2.
- $L = 1$: $b_1$ has 2 red neighbors, cost $= 2/2 = 1$.
- $L = 2$ (R–$b_1$–$b_2$–R): turning $b_1$ first costs $2/1 = 2$, then $b_2$ sees 2 red neighbors and costs $2/2 = 1$, total $= 3$.
- $L = 3$ (R–$b_1$–$b_2$–$b_3$–R): what is the best order and cost?
What pattern do you notice about which node benefits from being processed last?
Answer to Hint 5: For $L = 3$, process $b_1$ (cost 2), then $b_3$ (cost 2), then $b_2$ now has both neighbors red (cost $2/2 = 1$). Total $= 5$. Processing left-to-right gives $2 + 2 + 1 = 5$ as well (the last node always sees 2 reds).
The key pattern: the last node in a contiguous segment to be processed always finds both its neighbors already red, halving its cost. On a path, the order among earlier nodes doesn’t matter much, but in a tree a high-degree node can accumulate many red neighbors and see an even larger benefit. This is the core of the generalization.
On a tree, root each connected component of black nodes at an arbitrary node. For node $u$ with black children $v_1, \ldots, v_m$, you face a choice: which children should be turned red before $u$, and which after?
Think about the tradeoff. Turning child $v$ red before $u$ gives $u$ one more red neighbor when $u$ is processed, but $v$ is processed when its parent $u$ is still black. Conversely, turning $v$ red after $u$ means $v$ benefits from $u$ being red, but $u$ doesn’t benefit from $v$.
Can you formalize this with a DP?
Define $\mathrm{dp}[u][i]$ = minimum expected cost to turn the entire subtree of $u$ (within the black component tree) red, where $i \in \{0, 1\}$:
- $i = 0$: $u$’s parent in the black component tree has not been turned red yet.
- $i = 1$: $u$’s parent has already been turned red (giving $u$ one extra red neighbor).
Let $r_u$ = number of originally-red neighbors of $u$ (from the original tree) and $d_u$ = degree of $u$ (in the original tree). For a leaf of the black component tree (no black children):
- $\mathrm{dp}[u][0] = d_u / r_u$ (requires $r_u > 0$; otherwise $\infty$).
- $\mathrm{dp}[u][1] = d_u / (r_u + 1)$.
What about internal nodes?
Answer to Hint 8: Suppose $u$ has black children $v_1, \ldots, v_m$ and you choose $k$ of them to turn red before $u$. Then:
- The $k$ “early” children each cost $\mathrm{dp}[v][0]$ (their parent $u$ is still black).
- The remaining $m - k$ “late” children each cost $\mathrm{dp}[v][1]$ (their parent $u$ is now red).
- When $u$ is processed, it has $r_u + i + k$ red neighbors, costing $d_u / (r_u + i + k)$.
Total for a given choice of $k$ early children:
$$\frac{d_u}{r_u + i + k} + \sum_{\text{early } v} \mathrm{dp}[v][0] + \sum_{\text{late } v} \mathrm{dp}[v][1]$$Minimize over all valid $k$ and all choices of which children are early. Can you find an efficient way to pick which children should be early?
Start from the baseline where all children are late (total child cost $= \sum_v \mathrm{dp}[v][1]$). Switching child $v$ from late to early changes the child cost by $\delta_v = \mathrm{dp}[v][0] - \mathrm{dp}[v][1]$. Note that $\delta_v \ge 0$ always (having the parent red can only help $v$).
For a fixed $k$, you want the $k$ children with the smallest $\delta_v$ to minimize the extra cost. So sort children by $\delta_v$ ascending, and use prefix sums to evaluate each $k$ in $O(1)$.
Answer to Hint 10: Sort children by $\mathrm{dp}[v][0] - \mathrm{dp}[v][1]$ ascending into $\delta_1 \le \delta_2 \le \cdots \le \delta_m$. For each $k = 0, 1, \ldots, m$, the total cost is:
$$\frac{d_u}{r_u + i + k} + \sum_{j=1}^{m} \mathrm{dp}[v_j][1] + \sum_{j=1}^{k} \delta_j$$Take the minimum over all $k$ where $r_u + i + k > 0$. This is $O(m \log m)$ per node for the sort.
Answer to Hint 13: A component root has no parent in the black component tree, so there is no black parent that could “become red first” to help it. Its only red neighbors are those counted in $r_u$.
Overall complexity: $O(n \log n)$. Sorting children at each node dominates. Total children across all nodes in a tree is $O(n)$, so the total sorting work across the entire DFS is $O(n \log n)$.