Table of Contents

ExplanationImplementation

Official Editorial (C++)

Explanation

Without loss of generality, let's assume we've already computed the best beauty values for all of vertex uu's neighbors.

Let rangeu\texttt{range}_u store lul_u and rur_u and adju\texttt{adj}_u store the beauty values of all vertices adjacent to uu.

For example, we'll set

rangeu={1,10}\texttt{range}_u = \{1, 10\}

adju={2,3,6,9}\texttt{adj}_u = \{2, 3, 6, 9\}

Let's say that we select 77, which gives us a beauty of 72+73+76+79=12|7-2| + |7-3| + |7-6| + |7-9| = 12. Can we do better than this?

Intuitively, for any XX that we choose to be uu's beauty value, if there's more elements that are greater than this XX, making XX as small as possible would be more optimal (increasing differences for these elements) and vice versa. Note that in a case where the number of greater beauty values is the same as the number of smaller beauty values, setting XX to the minimum or maximum will never decrease the answer. Moving XX will always increase the distance for the other end of the beauty values.

Why does this work?

Choosing the median minimizes the sum of absolute deviation, so we'd want to get as far away from the median as possible.

This observation is all we need. Since setting uu to lul_u or rur_u is always optimal, we'll handle this through DP.

If dp[i][j]\texttt{dp}[i][j] stores the maximum sum of beauty values at the subtree rooted at ii when ii's "beauty type" is jj. Since vertex ii can only take two values, jj will be equal to 00 when uu's beauty value is lul_u and 11 when uu's beauty value is rur_u.

Our transitions are:

dpu 0+=max(dpv 0+rangeu 0rangev 0,dpv 1+rangeu 1rangev 0)\texttt{dp}_{u \space 0} \mathrel{+}= \max(\texttt{dp}_{v \space 0} + |range_{u \space 0} - range_{v \space 0}|, \texttt{dp}_{v \space 1} + |range_{u \space 1} - range_{v \space 0}|)

dpu 1+=max(dpv 1+rangeu 1rangev 1,dpv 0+rangeu 0rangev 1)\texttt{dp}_{u \space 1} \mathrel{+}= \max( \texttt{dp}_{v \space 1} + |range_{u \space 1} - range_{v \space 1}|, \texttt{dp}_{v \space 0} + |range_{u \space 0} - range_{v \space 1}|)

This essentially means:

If uu has the beauty lul_u:

dpu 0+=max(\texttt{dp}_{u \space 0} \mathrel{+}= \max(beauty if vv has a beauty value of lvl_v, beauty if vv has a beauty value of rvr_v)

If uu has the beauty rur_u:

dpu 1+=max(\texttt{dp}_{u \space 1} \mathrel{+}= \max(beauty if vv has a beauty value of lvl_v, beauty if vv has a beauty value of rvr_v)

Implementation

Time Complexity: O(N)\mathcal{O}(N)

C++

#include <algorithm>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::pair;
using std::vector;
vector<vector<int>> adj;

Java

import java.io.*;
import java.util.*;
public class ParsasHumongousTree {
private static List<List<Integer>> adj = new ArrayList<>();
private static int[] l, r;
private static long[][] dp;
public static void main(String[] args) {
Kattio io = new Kattio();

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!