Procvičujte diskrétní matematiku

Algorithms / Tree Dp Rerooting

Least You Need to Know: Tree DP, Child-State Combination, and Rerooting Intuition

Tree dynamic programming works because each node can summarize its subtree from its children. Rerooting extends that idea by reusing previously computed information when the root perspective shifts from one node to another.

The least you need to know

Key notation

dp_down[u] summary computed from the subtree of `u`
dp_up[u] summary contributed from outside the subtree of `u`
reroot transfer root perspective across an edge

Tiny worked example

  • Suppose `dp_down[u]` stores the longest downward path starting at `u`.
  • Combine child answers to compute the parent answer.
  • For all-roots answers, also send each child the best information coming from the parent side.
  • That reuse is rerooting: one global sweep down, one sweep back up, instead of starting over from each node.

Common mistakes

How to recognize this kind of problem

Start practice