Блог пользователя gelastropod

Автор gelastropod, 7 дней назад, По-английски

dynamic dynamic programming

Hello Codeforces!

In this blog, I will introduce Generalised Dynamic DP, a technique for handling DP on trees while the tree's values are being updated. Then, I will use it to solve a buffed version of 1787G - Colorful Tree Again.

This technique is well known in Chinese OI as 动态 DP (dynamic DP), introduced at WC2018; see the OI Wiki article for the standard treatment using $$$(\max,+)$$$ matrices. This blog generalises it to an arbitrary monoid of functions, and extends it to support range updates on paths.

Introduction

First, let's define a general tree DP. Consider a tree with $$$N$$$ vertices rooted at $$$r$$$. Let $$$\text{child}(v)$$$ be the set of children of $$$v$$$, and let $$$\text{dp}(v)$$$ be a value computed from the subtree of $$$v$$$, by

$$$\text{dp}(u)=f_u\big(\text{dp}(v)\mid v\in\text{child}(u)\big)$$$

for some function $$$f_u$$$. We also assume $$$f_u$$$ can be computed in time proportional to the number of values given to it. We want $$$\text{dp}(r)$$$, and this can be done in $$$O(N)$$$ time.

Now suppose we change one of the functions $$$f_u$$$ and want $$$\text{dp}(r)$$$ again. Naively, we need to recompute every $$$\text{dp}(v)$$$ that depends on $$$u$$$, namely all the ancestors of $$$u$$$. With $$$Q$$$ modifications, that is $$$O(NQ)$$$ time in the worst case. Here I present a way to speed this up to $$$O(N+Q\log^2N)$$$.

Monoids and segment trees

Before I introduce the technique, I would like to introduce the monoid. A monoid consists of a set $$$S$$$ and a binary operation $$$\cdot$$$, such that $$$S$$$ is closed under $$$\cdot$$$, $$$\cdot$$$ is associative, and $$$S$$$ contains an identity element $$$e$$$. In other words,

  1. For all $$$x,y\in S$$$, $$$x\cdot y\in S$$$.
  2. For all $$$x,y,z\in S$$$, $$$x\cdot(y\cdot z)=(x\cdot y)\cdot z$$$, so we can just write $$$x\cdot y\cdot z$$$.
  3. There exists an element $$$e\in S$$$ such that $$$e\cdot x=x\cdot e=x$$$ for all $$$x\in S$$$.

These are exactly the properties a segment tree needs! Because of associativity, it doesn't matter how we bracket a product $$$a_l\cdot a_{l+1}\cdot\ldots\cdot a_r$$$, so each segment tree node can store the product of its range, and we can handle updates and queries as usual. Note that commutativity ($$$x\cdot y=y\cdot x$$$) is not required: we only need to keep the elements in order.

One very useful example of a monoid is a set of functions under function composition. That is, we have a set $$$S$$$ of functions with $$$f\cdot g=f\circ g$$$ for $$$f,g\in S$$$. Function composition is always associative, so if $$$S$$$ is closed under composition and contains the identity function, then $$$(S,\cdot)$$$ is a monoid and can be used in a segment tree. For this to be useful, every function in $$$S$$$ should also be described by $$$O(1)$$$ values, so that composing two of them takes $$$O(1)$$$ time.

Some examples of $$$S$$$:

  • $$$S=\lbrace f_i\mid i\in\mathbb{Z}\rbrace$$$ where $$$f_i(x)=x+i$$$: $$$f_a\cdot f_b=f_{a+b}$$$, $$$e=f_0$$$.
  • $$$S=\lbrace g_{i,j}\mid i,j\in\mathbb{Z}\rbrace$$$ where $$$g_{i,j}(x)=ix+j$$$: $$$g_{a,b}\cdot g_{c,d}=g_{ac,\,ad+b}$$$, $$$e=g_{1,0}$$$. This one is not commutative.
  • $$$S=\lbrace h_i\mid i\in\mathbb{Z},\ i\ge0\rbrace$$$ where $$$h_i(x)=\max(x,i)$$$ on $$$\mathbb{Z}_{\ge0}$$$: $$$h_a\cdot h_b=h_{\max(a,b)}$$$, $$$e=h_0$$$.
EGOI 2026 1C

Monoids on HLD

Now let's return to the tree DP from the introduction. Perform heavy-light decomposition on the tree, so every non-leaf vertex $$$u$$$ has a heavy child $$$\text{hc}_u$$$. Separating $$$\text{hc}_u$$$ from the other children gives

$$$\text{dp}(u)=f_u\big(\text{dp}(\text{hc}_u),\ \text{dp}(v)\mid v\in\text{child}(u)\setminus\lbrace\text{hc}_u\rbrace\big).$$$

Since $$$f_u$$$ depends on $$$u$$$ anyway, we can "absorb" the light children's values into the function, to get a function of one argument: $$$\text{dp}(u)=g_u(\text{dp}(\text{hc}_u))$$$.

This is a much nicer form! Consider a heavy path (a path consisting only of heavy edges), and let $$$v$$$ be an ancestor of $$$u$$$ on it. Each transition $$$g_x$$$ is a function of the DP value just below it, so

$$$\text{dp}(v)=\big(g_v\circ g_{x_1}\circ g_{x_2}\circ\ldots\circ g_{p_u}\big)(\text{dp}(u)),$$$

where $$$v,x_1,x_2,\ldots,p_u$$$ are the vertices on the path from $$$v$$$ down to $$$u$$$'s parent ($$$p_u$$$), in order. If the functions $$$g_x$$$ all lie in a monoid under composition, we can store them in a segment tree over each heavy path. The bottom of every heavy path is a leaf, which has no heavy child, so we apply the whole product to a fixed base value $$$k$$$ (the DP value of an "empty" child). In particular, $$$\text{dp}(r)$$$ is the product over the heavy path containing $$$r$$$, applied to $$$k$$$.

To handle an update to some $$$f_u$$$, we first update $$$g_u$$$ in the segment tree, then go up from $$$u$$$. Every time we cross a light edge from a path head $$$h$$$ to its parent $$$p$$$ (this happens $$$O(\log N)$$$ times), we recompute $$$\text{dp}(h)$$$ from its path's product, then use it to update $$$g_p$$$ in the segment tree, and repeat until we reach the root's path. For this, we need to be able to update $$$g_p$$$ quickly when one light child's value changes, e.g. by keeping a sum, or a multiset if the operation can't be undone. Each update then takes $$$O(\log^2N)$$$ time, computing $$$\text{dp}(r)$$$ takes $$$O(1)$$$ (since it is just the root node in the segment tree), and everything uses $$$O(N)$$$ memory.

Sample problem

We will now use this technique to solve a classic problem:

Problem. You are given a tree with $$$N$$$ vertices and non-negative edge weights. Initially, all vertices are unmarked. You have to handle $$$Q$$$ queries of two types:

  1. Given a vertex $$$u$$$, flip the state of $$$u$$$: if $$$u$$$ is unmarked, mark it; if $$$u$$$ is marked, unmark it.
  2. Given a vertex $$$u$$$, find the distance from $$$u$$$ to the closest marked vertex.

The classic centroid decomposition solution takes $$$O(N\log N+Q\log^2N)$$$ time and $$$O(N\log N)$$$ memory. With this technique, we can solve the problem in the same time complexity, but with $$$O(N)$$$ memory.

First, consider finding the distance from a fixed vertex $$$r$$$ to the closest marked vertex. Root the tree at $$$r$$$ and define

$$$\text{dp}(u)=\begin{cases} 0, & u \text{ is marked},\\ \min_{v\in\text{child}(u)}\big(w_{u,v}+\text{dp}(v)\big), & \text{otherwise,} \end{cases}$$$

where $$$w_{u,v}$$$ is the weight of the edge between $$$u$$$ and $$$v$$$, and the minimum over no children is $$$\infty$$$. Then $$$g_u(x)=\min(x+w_{u,\text{hc}_u},\,c_u)$$$, where $$$c_u=0$$$ if $$$u$$$ is marked, and $$$c_u=\min_{v\in\text{child}(u)\setminus\lbrace\text{hc}_u\rbrace}(w_{u,v}+\text{dp}(v))$$$ otherwise. (When $$$u$$$ is marked, $$$\min(x+w,0)=0$$$ because the weights are non-negative, so this is still correct.)

So let $$$(a,b)$$$ represent the function $$$x\mapsto\min(x+a,b)$$$. Then

$$$(a,b)\cdot(c,d)=(a+c,\ \min(a+d,b)),\qquad e=(0,\infty),$$$

and the base value is $$$k=\infty$$$. Since $$$\min$$$ can't be undone, each vertex keeps a multiset of $$$w_{u,v}+\text{dp}(v)$$$ over its light children; these multisets have fewer than $$$N$$$ elements in total. Applying the technique, we can handle all updates and answer every query for the vertex $$$r$$$.

To solve the full problem, we first observe that counting a path more than once, or counting a walk that goes back on itself, does not change the answer: such a walk is never shorter than the real shortest path, which is still counted (similar to $$$\min(a,b,b)=\min(a,b)$$$). Let $$$A(x)$$$ be the true answer for vertex $$$x$$$. For each vertex $$$x$$$ with parent $$$p$$$, we also store a "reverse" function $$$\rho_x(y)=\min(y+w_{p,x},\,c_x)$$$, which is a function of the value coming from the parent instead of from the heavy child. It has the same $$$(a,b)$$$ form, so it lives in the same monoid, and we keep it in a second segment tree, composed in the opposite order.

Now walk down from $$$r$$$ to $$$u$$$. When we enter a heavy path at its head $$$h$$$, coming from $$$q=\text{parent}(h)$$$, and leave it at $$$x$$$:

  • $$$(\rho_x\circ\ldots\circ \rho_h)(A(q))$$$ covers everything reached by going up from $$$x$$$ along the path, or out of $$$q$$$;
  • $$$\text{dp}(x)$$$, the product of $$$g$$$ from $$$x$$$ down to the end of the path, covers the subtree of $$$x$$$;

so $$$A(x)$$$ is the minimum of the two. Here $$$c_q$$$ also includes the light child $$$h$$$ we came from, but by the observation above this is harmless. Each heavy path costs $$$O(\log N)$$$, so a query takes $$$O(\log^2N)$$$. An update changes $$$c_x$$$ for $$$O(\log N)$$$ vertices, and each change updates both $$$g_x$$$ and $$$\rho_x$$$. Therefore we achieve the time and memory complexity stated above.

Solving buffed 1787G

This is 1787G - Colorful Tree Again, but with path assignments instead of point updates, and asking for the sum of the good colours instead of the maximum:

Problem. You are given a tree with $$$N$$$ vertices. Each edge has a weight $$$w$$$ and a colour $$$c$$$. For each colour $$$c$$$, let $$$W_c$$$ be the total weight of the edges of colour $$$c$$$. A colour is valid if its edges form a single simple path.

Initially, every vertex is unblocked. You have to handle $$$Q$$$ queries p u v:

  • if $$$p=0$$$, every vertex on the path from $$$u$$$ to $$$v$$$ becomes blocked;
  • if $$$p=1$$$, every vertex on the path from $$$u$$$ to $$$v$$$ becomes unblocked.

These are assignments: a vertex blocked twice and unblocked once is unblocked. A valid colour is good if no vertex on its path is blocked. After each query, output the sum of $$$W_c$$$ over all good colours.

Input. The first line contains $$$N$$$ and $$$Q$$$ ($$$2\le N\le2\cdot10^5$$$, $$$1\le Q\le2\cdot10^5$$$). Each of the next $$$N-1$$$ lines contains $$$u,v,w,c$$$ ($$$1\le u,v\le N$$$, $$$u\ne v$$$, $$$1\le w\le10^9$$$, $$$1\le c\le N$$$), an edge between $$$u$$$ and $$$v$$$ with weight $$$w$$$ and colour $$$c$$$. These edges form a tree. Each of the next $$$Q$$$ lines contains $$$p,u,v$$$ ($$$p\in\lbrace0,1\rbrace$$$, $$$1\le u,v\le N$$$).

Output. After each query, output the answer on its own line.

Sample

Try it yourself before reading on! This can be solved in $$$O(N+Q\log^2N)$$$ time and $$$O(N)$$$ memory.

Solution

Conclusion

To summarise, generalised dynamic DP works whenever:

  1. The transition at $$$u$$$, with the light children absorbed, is a function of the heavy child's value;
  2. These functions lie in a monoid under composition, with an $$$O(1)$$$ description;
  3. The absorbed light-child data can be updated quickly when one light child changes.

Then point updates take $$$O(\log^2N)$$$, and path updates may also work. If composing two functions cost $$$T$$$ (e.g. $$$T=k^3$$$ for $$$k\times k$$$ matrices), then the time complexity is also multiplied by $$$T$$$.

Thanks for reading! :]

  • Проголосовать: нравится
  • +23
  • Проголосовать: не нравится

»
13 минут назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Whenever you're posting a draft, please create a new blog, paste the draft's contents into it, and then post the new blog. This blog, which I believe was posted in the last few hours, currently shows "7 days ago", because that's when you created the draft. This is confusing/off-putting.

  • »
    »
    9 минут назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    alright