# Hagen Farm Editorial **Two methods: the problem setter’s subtree expectation DP, and edge-probability summation derived by Codex (GPT-6).** Both take $O(n^2)$ time. The edge method reduces runtime space from $O(n^2)$ to $O(n)$ , so it is used in `std.cpp` . Subtree DP retains intermediate results by selected-set size. Both methods, their proofs, and their uses are described below; the choice does not rely on unmeasured runtime comparisons. ## 1. The shortest closed walk The minimal subtree connecting all apple vertices is unique. A closed walk using one of its edges must cross that edge and return, so it traverses every such edge at least twice. A traversal of this subtree attains the bound. Thus the distance is twice its total edge length. A single apple contributes zero. By linearity of expectation, sum the contribution of each edge. ## 2. Edge-inclusion probability for a fixed generation subtree Consider the edge to a child $v$ whose subtree size is $t$ . It can be used only if the randomly chosen generation root $u$ is a strict ancestor of $v$ . Write $s$ for the size of $u$’s subtree. The edge separates this range into parts of sizes $t$ and $s-t$ . With a fixed number $k$ of apples, the edge is excluded exactly when all apples lie on one side. These events are disjoint for $k\ge1$ , so the inclusion probability is $$1-\frac{\binom tk+\binom{s-t}k}{\binom sk}$$ Treat a binomial coefficient as zero if its lower index exceeds the available size. Average over uniform $k=1,\ldots,s$ . For $0\le t0]$ . Sum this expression with the above weights over valid $j$ . Conditional on the counts, choices within each part remain uniform, proving the transition. After computing every $D_v(k)$ , define $$L_v(k)=\binom{s_v}kD_v(k)-\sum_{c\text{ is a child of }v}\binom{s_c}k\bigl(D_c(k)+w_c\bigr)$$ Only include terms with $k\le s_c$ . This is the total minimal-subtree length over size-$k$ subsets whose lowest common ancestor is exactly $v$ . If all selected vertices lie in one child subtree, the subtraction removes their entire forced connection to $v$ . Every other subset has a minimal connecting subtree through $v$ . Such a subset can be generated from any ancestor subtree containing $v$ . During a root-to-vertex traversal, maintain $$W_v(k)=\sum_{u\text{ is an ancestor of }v\text{ or }u=v}\frac1{s_u\binom{s_u}k}$$ Include only terms with $k\le s_u$ . Add or remove a vertex’s contribution on entry or exit. The final result is $$E=\frac2n\sum_v\sum_{k=1}^{s_v}L_v(k)W_v(k)$$ Every nonempty apple set has a unique lowest common ancestor, so it is counted once. Precompute binomial coefficients and their inverses. Nontrivial merge-state pairs can be charged to the first merge joining a pair of vertices, for $O(n^2)$ total work; the remaining state scans also total at most $O(n^2)$ . Both time and table space are $O(n^2)$ . These DP states are useful if a variant asks about a particular subtree and a fixed selected count $k$ . For the single expectation under the stated distribution, edge probabilities avoid the two-dimensional tables. ## Data and validation `tests/generate.py` covers chains, stars, balanced trees, brooms, zero-length edges, and random trees. The independent small-instance oracle enumerates the generation root, apple count, and every subset, then directly finds the connecting edges. See [environment and measurements](../../../docs/评测环境与性能.md).