Let be the vertices on the path from Core to Core , in order. Assign each vertex to its unique closest vertex on the Core path, and let be the number assigned to . This group includes itself, so every is positive.
Only edges on the Core path may be deleted. Consequently, vertices in the same group always stay together, and each resulting component consists of a contiguous interval of groups. Conversely, any partition of the groups into exactly nonempty contiguous intervals can be obtained by deleting the edges between those intervals.
A pair of vertices in the same group contributes zero energy. For two different groups and , there are vertex pairs, each contributing . Therefore, the cost of making groups through one component is
To find the groups, root the original tree at and compute each vertex's parent together with a traversal order in which every parent precedes its children. Follow parents from to recover the Core path. First assign group indices to all path vertices. Then traverse the other vertices in the recorded order and give each vertex its parent's group index. This takes time.
Define the following prefix sums, with all values at index zero equal to zero:
When adding group , only pairs between that group and earlier groups are new. Thus,
Set . The difference includes both the cost within and the cost of pairs with one group in and the other in . Subtracting the latter gives
After preprocessing, this formula evaluates any interval cost in .
Let be the minimum cost of partitioning the first groups into exactly nonempty contiguous intervals. Enumerating the last interval gives
Initialize and all impossible states to infinity. The answer is . Directly evaluating this recurrence takes time.
For , expanding the following difference yields
Thus, the interval costs satisfy the Monge inequality. Adding to each candidate preserves this inequality. It follows that the optimal starting position of the last interval does not decrease as increases. Choosing the smallest starting position in a tie gives a consistent nondecreasing sequence of optimal positions.
To compute one layer, first process the middle endpoint . If the possible optimal starting positions lie in , inspect from through and find an optimum . Recurse on the left half with candidate range , and on the right half with candidate range . For layer , start with endpoint range and candidate range . Ignore candidates whose previous-layer state is impossible.
The recursion has levels, and the total number of inspected candidates at each level is . One layer therefore takes time.
The total time complexity is , and the space complexity is . Only the previous and current layers need to be stored.
There are at most vertex pairs, and each pair contributes at most , so the total energy is less than . Also, and , so . The intermediate sums and differences in the cost formula fit in 64-bit integers as well. Use 64-bit integers for , , , , the values, and intermediate products. The original tree being unweighted does not make a 32-bit answer sufficient.
The tree can be a path with vertices. An iterative traversal avoids a stack overflow while building the groups. The divide-and-conquer itself has only recursion depth.