Editorial : H. Fallen Leaves
You have a tree on $n\ge 3$ vertices. Let $S$ be its leaf set: vertices of degree at most one (this matches Codeforces’ definition used in the statement).
You repeatedly choose two unused leaves $u,v\in S$, pay $\mathrm{dist}(u,v)$, and mark both as used. Stop when at most one leaf in $S$ remains unused.
Goal: minimize total paid distance.
Official tutorial (same contest): Codeforces Round 1096 (Div. 3) — Editorial.
Take any multiset of pairwise disjoint unordered pairs of vertices $(u_k,v_k)$ (here each endpoint lies in $S$).
$$ \sum_k \mathrm{dist}(u_k,v_k)=\sum_{\text{edges }e\in E}\bigl(\#\text{ pairs whose unique path uses }e\bigr). $$So every algorithm that produces the same multiset of pairs pays the same total. Conversely, minimizing total distance is purely about controlling how many paired paths traverse each edge.
This “edge incidence” viewpoint is the whole conceptual shortcut.
Pick any root $r$. Parent pointers induce subtrees.
Define $\mathrm{cnt}[x]$: number of vertices of $S$ in the subtree of $x$ (including $x$ if $x\in S$).
Compute all $\mathrm{cnt}[\cdot]$ in $\mathcal O(n)$ by DFS.
Assume $|S|$ is even and eventually every leaf gets paired.
Define
$$ B=\sum_{\text{edges }(\mathrm{parent},c)} (\mathrm{cnt}[c]\bmod 2). $$Fix rooted orientation from $r$. Take any feasible pairing $\mathcal P$.
For downward edge $(p,c)$ let $k_{\mathcal P}(c)$ be how many pairs of $\mathcal P$ route through this edge.
Only pairs splitting one leaf in $T_c$ from its partner outside contribute.
Let $t_{\mathcal P}(c)$ count leaf endpoints inside $T_c$ whose partner lies outside $T_c$. Then $k_{\mathcal P}(c)=t_{\mathcal P}(c)$ (tree uniqueness of routing).
Because endpoints inside $T_c$ partition into internally paired endpoints (even contribution) vs externally paired endpoints ($t_{\mathcal P}(c)$ singleton leaves crossing),
$$ t_{\mathcal P}(c)\equiv \mathrm{cnt}[c]\pmod 2. $$Hence $t_{\mathcal P}(c)\ge \mathrm{cnt}[c]\bmod 2$ as nonnegative integers.
Summing edges yields $\mathrm{cost}(\mathcal P)=\sum_e k_{\mathcal P}(e)\ge B$.
Important nuance: Nothing stops $t_{\mathcal P}(c)$ from equalling $3,5,\ldots$ when $\mathrm{cnt}[c]$ is odd — cheapest feasible routing tries to avoid wasting repeated crossings.
Example attachment for intuition only: hang leaves $x,y,z$ off one hub below $(p,c)$ and pair each with distant outside partners — $t_{\mathcal P}(c)=3$, overshooting parity minimum.
Build an explicit pairing costing exactly $B$ by a one-pass greedy on the rooted tree:
Process children subtrees recursively; each returns at most one still-unpaired leaf needing a partner strictly higher toward the root (the standard “matching excess flow upward” viewpoint on trees).
Whenever two unmatched leaves meet at the same node, pair them immediately — their connecting path lies completely in processed regions, avoids duplicating charges on deeper edges unnecessarily.
Because $|S|$ is even, nothing remains after the root aggregates.
One can inductively verify each edge $(p,c)$ ends up charged exactly $\mathrm{cnt}[c]\bmod 2$ times by this construction.
Combining bounds, the minimum possible total equals $B$.
Now $|S|$ is odd; exactly one leaf $x\in S$ stays unused.
Interpret unused leaf as deleting $x$ from the pairing requirement while keeping the same geometric tree.
Compare crossing parity edge-by-edge versus the hypothetical world where $|S|$ were even except parity bookkeeping treats subtrees differently whenever membership of $x$ flips containment.
Along fixed root $r$, consider downward edge $(p,c)$ where $c$ is child.
Original parity classification uses odd/even of $\mathrm{cnt}[c]$.
After declaring $x$ unmatched:
- Subtrees disjoint from $x$ behave unchanged.
- Along root$\to$$x$, each downward pointer switches whether subtree contains $x$, flipping parity contributions edge-by-edge relative baseline $\mathrm{cnt}[c]\bmod 2$.
Thus total distance equals baseline sum $B$ minus savings computed purely from toggling edges on root$\to$$x$ path.
Codify toggling weights:
$$ w(p,c)= \begin{cases} +1,& \mathrm{cnt}[c]\text{ odd},\\ -1,& \mathrm{cnt}[c]\text{ even.} \end{cases} $$Define leaf score
$$ \mathrm{score}(x)=\sum_{e\text{ on path } r\to x} w(e). $$Maximize $\mathrm{score}(x)$ over $x\in S$ — equivalently maximize savings — yielding minimum total:
$$ \text{answer}_{\mathrm{odd}} = B-\max_{x\in S}\mathrm{score}(x). $$DFS DP:
For leaf $\ell$, $\mathrm{best}[\ell]=0$.
Otherwise for vertex $u$ with children $c$:
$$ \mathrm{best}[u]=\max_{\text{children }c}\bigl(\mathrm{best}[c]+w(u,c)\bigr). $$Answer $\max_{x\in S}\mathrm{score}(x)=\mathrm{best}[r]$.
Overall $\mathcal O(n)$ per test case.
The optimal objective here collapses to parity bookkeeping plus one path maximization. Greedy constructions that ignore those invariants mislead intuition even when they feel geometrically sensible.
Repeatedly pairing two leaves of minimum current distance can fail.
Informally: distances satisfy tree ultrametric constraints poorly — committing early to a cheap local chord may destroy an alternative rearrangement where slightly longer chords eliminate overlapping usage of expensive deep backbone edges.
Concrete reasoning anchor: experiment on trees shaped like a long spine with short branches holding dense clusters of leaves. Minimum-distance greedy tends to marry neighbors within each cluster first; optimal global pairing often sends leaves across clusters so crossings concentrate on cheaper spine segments rather than multiplying expensive detours.
Students implementing purely distance-heuristic greedy typically succeed only accidentally when parity collapses coincidentally.
Symmetric pitfall: pairing extremes early ignores which leaf remains unmatched when $|S|$ is odd — odd-case optimum equals $B-\max \mathrm{score}$, i.e.\ structural savings concentrated along one root path, not Euclidean-style extremes.
Computing even-$|S|$ formula $B$ blindly — forgetting subtraction $\max \mathrm{score}$ — violates Step 4 exactly when parity breaks globally.
Commentators note greedy simulation pitfalls (discussion thread): without parity abstraction, reasoning degenerates into casework unrelated to polynomial-time DP above.
The checked-in model_sol.cpp differs syntactically from the editorial snippet but computes identical answers: verified against the tutorial recurrence on all bundled samples.
Rather than accumulating $B$ explicitly then subtracting a root DP, it merges subtrees bottom-up maintaining $2\times 2$ tables encoding:
- Whether the partially processed subtree currently contributes parity-driven obligation crossing upward toward its parent (only $\{0,1\}$ suffices — merges forbid contradictory simultaneous upward obligations without reconciliation).
- A XOR-tracked parity flag capturing whether the accumulated baseline expects charging the connecting edge consistent with $\mathrm{cnt}\bmod 2$ bookkeeping.
Final extraction takes $\min$ among upward-compatible zero-parity completions — aligning with even-case minimization plus encoded odd-case subtraction.
Readers comfortable only with closed forms may implement $B+\mathrm{best}$ DP; readers preferring compact merges may study model_sol.cpp after mastering Steps 3–4.
Time $\mathcal O(n)$ per test case, memory $\mathcal O(n)$.
Summed $n\le 2\cdot 10^5$ across tests fits easily.