Skip to content

P4775 NOI2018 情报中心

Step 1

We need to: 1. choose 2 given paths 2. paths share an edge 3. maximize value = sum of edges chosen - sum of cost

We can first calculate its own value for every path. Now the problem becomes how to merge.

What does it mean for 2 paths to share at least one edge?

Hint / Intuition

Let \(s_1\) and \(s_2\) be the lower end of each path. $$ \begin{align} 2\text{ANS}E &= c(s_1, s_2) + c(t_1, t_2) + (c(s_1, t_1) - 2w_1) + (c(s_2, t_2) - 2w_2) \ &= c(s_1, s_2) + c(t_1, t_2) + f{(s_1, t_1)} + f_{(s_2, t_2)} \end{align} $$ If we record the answer at \(\text{lca}(s_1, s_2)\), then we also guarantee the path will join.

Solution

There are 2 distances in the expression, therefore we must break one of then up. We can iterate over \(v=\text{lca}(s_1, s_2)\), then we have: $\((\text{dep}(s_1) + f_{(s_1, t_1)}) + (\text{dep}(s_2) + f_{(s_2, t_2)}) + c(t_1,t_2) - 2\text{dep}(v)\)$

The \(-2\text{dep}(v)\) is a constant and can be ignored. Now we have reduced to problem to asking: $\(\max(c(t_1, t_2) + w(t_1) + w(t_2))\)$ This is the weighted diameter and it has the same merging properties as a typical diameter.

Now we want to do the following:

For each \(v\), insert all paths with \(s\) inside subtree \(v\). Then for all the corresponding \(t\)s, calculate their max weighted diameter \(D\). We max the answer with \(D - 2\text{dep}(v)\). \(v\) might not be the LCA for all pairs of \(s\) inside \(v\), but the answers they produce are not optimal (i.e. moving lower in the tree will yield a better answer).

Essentially, we need to maintain a dynamic diameter. And this is trivial with segment tree merge.