Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,39 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:树形动态规划

对每个节点 $u$,我们维护一个二维数组 $f_u[j][pre]$,表示在以 $u$ 为根的子树中,预算不超过 $j$ 且 $u$ 的上司是否购买了股票(其中 $pre=1$ 表示购买,而 $pre=0$ 表示未购买)的情况下,可以获得的最大利润。那么答案就是 $f_1[\text{budget}][0]$。

对节点 $u$,函数 $\text{dfs}(u)$ 返回一个 $(\text{budget}+1) \times 2$ 的二维数组 $f$,表示在以 $u$ 为根的子树中,不超过预算 $j$ 且 $u$ 的上司是否购买了股票的情况下,可以获得的最大利润。

对 $u$,我们要考虑两件事:

1. 节点 $u$ 本身是否买股票(会占用一部分预算 $\text{cost}$,其中 $\text{cost} = \lfloor \text{present}[u] / (pre + 1) \rfloor$)。并增加利润 $\text{future}[u] - \text{cost}$。
2. 节点 $u$ 的子节点 $v$ 如何分配预算以最大化利润。我们把每个子节点的 $\text{dfs}(v)$ 看成“物品”,用背包把子树的利润合并到当前 $u$ 的 $\text{nxt}$ 数组中。

具体实现时,我们先初始化一个 $(\text{budget}+1) \times 2$ 的二维数组 $\text{nxt}$,表示当前已经合并了子节点的利润。然后对于每个子节点 $v$,我们递归调用 $\text{dfs}(v)$ 得到子节点的利润数组 $\text{fv}$,并用背包把 $\text{fv}$ 合并到 $\text{nxt}$ 中。

合并公式为:

$$
\text{nxt}[j][pre] = \max(\text{nxt}[j][pre], \text{nxt}[j - j_v][pre] + \text{fv}[j_v][pre])
$$

其中 $j_v$ 表示分配给子节点 $v$ 的预算。

合并完所有子节点后的 $\text{nxt}[j][pre]$ 表示在 $u$ 本身尚未决定是否购买股票的情况下,且 $u$ 的上次购买状态为 $pre$ 时,把预算 $j$ 全部用于子节点所能获得的最大利润。

最后,我们决定 $u$ 是否购买股票。

- 如果 $j \lt \text{cost}$,则 $u$ 无法购买股票,此时 $f[j][pre] = \text{nxt}[j][0]$。
- 如果 $j \geq \text{cost}$,则 $u$ 可以选择购买或不购买股票,此时 $f[j][pre] = \max(\text{nxt}[j][0], \text{nxt}[j - \text{cost}][1] + (\text{future}[u] - \text{cost}))$。

最后返回 $f$ 即可。

答案为 $\text{dfs}(1)[\text{budget}][0]$。

时间复杂度 $O(n \times \text{budget}^2)$,空间复杂度 $O(n \times \text{budget})$。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,39 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Tree Dynamic Programming

For each node $u$, we maintain a 2D array $f_u[j][pre]$, representing the maximum profit that can be obtained in the subtree rooted at $u$ with a budget not exceeding $j$ and whether $u$'s manager purchased stocks (where $pre=1$ means purchased, and $pre=0$ means not purchased). The answer is $f_1[\text{budget}][0]$.

For node $u$, the function $\text{dfs}(u)$ returns a $(\text{budget}+1) \times 2$ 2D array $f$, representing the maximum profit that can be obtained in the subtree rooted at $u$ with a budget not exceeding $j$ and whether $u$'s manager purchased stocks.

For $u$, we need to consider two things:

1. Whether node $u$ itself buys stocks (which will consume part of the budget $\text{cost}$, where $\text{cost} = \lfloor \text{present}[u] / (pre + 1) \rfloor$), and increase the profit by $\text{future}[u] - \text{cost}$.
2. How to allocate the budget among node $u$'s child nodes $v$ to maximize profit. We treat each child node's $\text{dfs}(v)$ as an "item" and use a knapsack approach to merge the subtree profits into the current $u$'s $\text{nxt}$ array.

In the specific implementation, we first initialize a $(\text{budget}+1) \times 2$ 2D array $\text{nxt}$, representing the profits that have been merged from child nodes. Then for each child node $v$, we recursively call $\text{dfs}(v)$ to get the profit array $\text{fv}$ of the child node, and use a knapsack approach to merge $\text{fv}$ into $\text{nxt}$.

The merge formula is:

$$
\text{nxt}[j][pre] = \max(\text{nxt}[j][pre], \text{nxt}[j - j_v][pre] + \text{fv}[j_v][pre])
$$

where $j_v$ represents the budget allocated to child node $v$.

After merging all child nodes, $\text{nxt}[j][pre]$ represents the maximum profit that can be obtained by allocating the entire budget $j$ to child nodes when $u$ itself has not yet decided whether to buy stocks, and $u$'s manager's purchase state is $pre$.

Finally, we decide whether $u$ purchases stocks.

- If $j \lt \text{cost}$, then $u$ cannot purchase stocks, in which case $f[j][pre] = \text{nxt}[j][0]$.
- If $j \geq \text{cost}$, then $u$ can choose to purchase or not purchase stocks, in which case $f[j][pre] = \max(\text{nxt}[j][0], \text{nxt}[j - \text{cost}][1] + (\text{future}[u] - \text{cost}))$.

Finally, return $f$.

The answer is $\text{dfs}(1)[\text{budget}][0]$.

The time complexity is $O(n \times \text{budget}^2)$, and the space complexity is $O(n \times \text{budget})$.

<!-- tabs:start -->

Expand Down