求含k个蓝色节点的完美二叉树最优顶点覆盖的动态规划解法
Got it, let's work through how to solve this constrained vertex cover problem on a perfect binary tree using dynamic programming. First, let's clarify the problem to make sure we're on the same page: we need to find the smallest possible vertex cover of a perfect binary tree, where the vertex cover must include exactly k blue nodes (the rest can be black nodes, as long as the vertex cover condition is satisfied).
Since we're dealing with a tree, we can define our DP states recursively based on each node and its children. For every node u, we'll track two key scenarios: whether u is included in the vertex cover or not, along with the number of blue nodes selected in its subtree and the corresponding minimum size of the vertex cover.
We define two 2D arrays for each node u:
dp[u][0][c]: The minimum size of a vertex cover for the subtree rooted atu, whereuis not included in the cover, and exactlycblue nodes are selected in this subtree.dp[u][1][c]: The minimum size of a vertex cover for the subtree rooted atu, whereuis included in the cover, and exactlycblue nodes are selected in this subtree.
Note: c ranges from 0 to the total number of blue nodes in the subtree rooted at u (we can precompute this value for each node to avoid invalid state checks). We'll initialize all states to infinity (∞) to represent impossible scenarios, then fill in valid values as we compute.
For a leaf node u (which has no children):
- If we don't include
uin the cover:dp[u][0][0] = 0(no nodes selected, 0 blue nodes, valid since a leaf has no edges to cover)
- If we include
uin the cover:- If
uis blue:dp[u][1][1] = 1(we select this 1 blue node, cover size is 1) - If
uis black:dp[u][1][0] = 1(we select this black node, 0 blue nodes, cover size is 1)
- If
- All other
cvalues fordp[u][1][c]remain∞(since we can't select a different number of blue nodes from a single leaf).
Let v be the left child of u, and w be the right child of u (since it's a perfect binary tree, every non-leaf has exactly two children).
Scenario 1: u is not included in the cover
If we don't select u, we must select both v and w (to cover the edges u-v and u-w). We need to combine the states where v and w are selected, summing their blue node counts to c:
dp[u][0][c] = min{ dp[v][1][c1] + dp[w][1][c2] | c1 + c2 = c }
We only update this value if there exists valid c1 and c2 such that their sum equals c (otherwise, it stays ∞).
Scenario 2: u is included in the cover
If we select u, we can choose to include or exclude v and w (since the edges u-v and u-w are already covered by u). We first calculate the blue node contribution from u itself:
add = 1ifuis blue, else0
We then need to find pairs of c1 (blue nodes in v's subtree) and c2 (blue nodes in w's subtree) such that c1 + c2 + add = c (i.e., c1 + c2 = c - add). The minimum cover size is the smallest sum of valid states from v and w, plus 1 (for including u):
dp[u][1][c] = min{ dp[v][0][c1] + dp[w][0][c2], dp[v][0][c1] + dp[w][1][c2], dp[v][1][c1] + dp[w][0][c2], dp[v][1][c1] + dp[w][1][c2] } + 1
Again, this only applies if c - add ≥ 0 and there exist valid c1/c2 pairs; otherwise, the state remains ∞.
Once we've computed the DP states for the root node r, the answer is the minimum value between:
dp[r][0][k]: The smallest vertex cover where the root is not included, with exactlykblue nodes.dp[r][1][k]: The smallest vertex cover where the root is included, with exactlykblue nodes.
If one of these values is ∞, it means that scenario is impossible, so we take the other valid value. If both are ∞, there is no valid vertex cover that meets the requirement (though the problem likely assumes a solution exists).
- Precompute
blue_count[u]for each nodeu(the total number of blue nodes in its subtree) to limit the range ofcwe need to process, which reduces redundant calculations. - For the state transitions that involve summing
c1andc2, we can optimize this using a knapsack-like approach to avoid checking all possible pairs naively, bringing the time complexity to O(n*K), wherenis the number of nodes andKis the total number of blue nodes in the tree.
内容的提问来源于stack exchange,提问作者user12400828

