Skip to content

P6773 NOI2020 命运

Step 1

Naïve DP: We define the dp state \(f[u,d]\) to be the answer when I solved everything in the subtree \(u\) and there is a condition that needs a colored edge below \(d\). This depth is the depth of the lower node in the edge. Let's also denote an edge by it's lower node. The constraint \(c(u)\) will be put on the lower node as well. \(\text{dep}(\text{root}) = 1\).

If we choose to color \(u\): $\(f_1[u,1] = \prod_{r[v] = u}{\sum_{1 \le d \le \text{dep}(u)}{f[v, d]}}\)$ $\(f_1[u, 1 < d \le \text{dep}(u)] = 0\)$

If we choose not to color \(u\): $\(f_2[u, c(u)] = \prod_{r[v] = u}{\sum_{1 \le d \le c(u)}{f[v, d]}}\)$ $\(f_2[u, c(u) < d \le \text{dep}(u)] = \prod_{r[v] = u}{\sum_{1 \le d' \le d}{f[v, d']}} - \prod_{r[v] = u}{\sum_{1 \le d' < d}{f[v, d']}}\)$ (This is calculating everything \(\le d\), then excluding the ones with non-\(d\) maximum.) $\(f_2[u, 1 \le d < c(u)] = 0\)$ $\(f[u, d] = f_1[u, d] + f_2[u, d]\)$ \(\mathcal{O}(n^3)\).

Step 2

Let \(g[u, d] = \sum_{1 \le d' \le d}{f[u, d']}\), then: $\(f_1[u, 1] = \prod_{r[v] = u}{g[v, \text{dep}(u)]}\)$ $\(f_2[u, c(u)] = \prod_{r[v] = u}{g[v, c(u)]}\)$ $\(f_2[u, c(u) < d \le \text{dep}(u)] = \prod_{r[v] = u}{g[v, d]} - \prod_{r[v] = u}{g[v, d - 1]}\)$ $\(g_1[u, d] = \prod_{r[v] = u}{g[v, \text{dep}(u)]}\)$ $\(g_2[u, 1 \le d < c(u)] = 0\)$ $\(g_2[u, c(u) \le d \le \text{dep}(u)] = \prod_{r[v] = u}{g[v,d]}\)$ $\(g[u,d] = g_1[u,d] + g_2[u,d] = \prod_{r[v] = u}{g[v, \text{dep}(u)]} + \begin{cases} \prod_{r[v] = u}{g[v,d]}, & c(u) \le d \le \text{dep}(u) \\ 0, & 1 \le d < c(u) \end{cases}\)$ $\(\text{ANS} = g[\text{root}, 1]\)$ \(\mathcal{O}(n^2)\).

Step 3

We can use segment tree merge to optimize.

At each leaf node \(u\), we can initiate a dynamic segment tree with range \([1 \dots \text{maxdep}]\) and initialized to \(0\). We can do range set on \([1 \dots c(u) - 1] \rightarrow 1\) and \([c[u] \dots \text{dep}(u)] \rightarrow 2\). This is \(\mathcal{O}(\log n)\).

When we are at a non-leaf node \(u\), we need to accumulate its children. 1. Calculate \(g_1 = \prod_{r[v] = u}{g[v, \text{dep}(u)]}\) in \(\mathcal{O}((\# \text{ of children}) \log n)\). 2. Calculate pointwise multiplication for all of its children in \(\mathcal{O}((\# \text{ of children}) \log n)\) (segment tree merge sequentially). 3. Set \([1 \dots c(u) - 1]\) to \(0\). \(\mathcal{O}(\log n)\). (We can also do this for everything after \(\text{dep}(u)\), but it won't affect its time complexity.) 4. Add \(g_1\) to \([1 \dots \text{dep}(u)]\) \(\mathcal{O}(\log n)\). (Or we can do this for the entire segment tree.)

This is \(\mathcal{O}(n \log n)\).

We need careful handling of the lazy tags. Range operations include add, set and multiply (for merging). We can maintain values directly with lazy tags.