PrevNext
Rare
 0/6

DP on Trees - Combining Subtrees

Author: Benjamin Qi

?

StatusSourceProblem NameDifficultyTagsSolution
CFNormalCheck CF

This was the first problem I saw that involved this trick.

Solution

For two vectors aa and bb, define the vector c=abc=a\oplus b to have entries ci=mink=0i(ak+bik)c_i=\min_{k=0}^i\left(a_k+b_{i-k}\right) for each 0i<size(a)+size(b)0\le i < \text{size}(a)+\text{size}(b).

Similar to the editorial, define dp[x][0][g]\texttt{dp[x][0][g]} to be the minimum cost to buy exactly gg goods out of the subtree of xx if we don't use the coupon for xx, and define dp[x][1][g]\texttt{dp[x][1][g]} to be the minimum cost to buy exactly gg goods out of the subtree of xx if we are allowed to use the coupon for xx. We update dp[x][0]\texttt{dp[x][0]} with one of the child subtrees tt of xx by setting dp[x][0]=dp[x][0]dp[t][0]\texttt{dp[x][0]}=\texttt{dp[x][0]}\oplus \texttt{dp[t][0]}, and similarly for dp[x][1]\texttt{dp[x][1]}.

Code

The editorial naively computes a bound of O(N3)O(N^3) on the running time of this solution. However, this actually runs in O(N2)O(N^2)!

Time Complexity of Merging Subtrees

This section is not complete.

Feel free to file a request to complete this using the "Contact Us" button.

add proof

Problems

StatusSourceProblem NameDifficultyTagsSolution
COCINormal
CFNormalCheck CF
DMOJNormal
CFNormal
Show Tags

DP

DMOJHard
Show Tags

NT

Give Us Feedback on DP on Trees - Combining Subtrees!

Module Progress:

PrevNext