DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Paths with Equal Ends

Back to problem

Editorial

Root the tree at 111 and process children before their parents. Let CvC_vCv​ be the minimum number of paths covering the subtree of vvv. Let Ov(c)O_v(c)Ov​(c) be the minimum number of closed paths when one open path, with endpoint vvv and value ccc at its other endpoint, is left available for extension toward the parent. The open path itself is not counted.

For each child uuu, define Du(c)=Cu−Ou(c)D_u(c)=C_u-O_u(c)Du​(c)=Cu​−, and let . Ignore unattainable states. Compute

gv=max⁡(0, max⁡uDu(Av), max⁡u≠w, c(Du(c)+Dw(c))),Cv=Bv+1−gv.g_v=\max\left(0,\ \max_u D_u(A_v),\ \max_{u\ne w,\,c}\bigl(D_u(c)+D_w(c)\bigr)\right), \qquad C_v=B_v+1-g_v.gv​=

Let Mv(c)M_v(c)Mv​(c) be the largest child gain Du(c)D_u(c)Du​(c), also considering 00 when . Then

Dv(c)=1−gv+Mv(c).D_v(c)=1-g_v+M_v(c).Dv​(c)=1−gv​+

Keep negative gains as well.

Store each DvD_vDv​ in a map keyed by ccc. At vvv, start with the temporary map {Av:0}\{A_v:0\}{A and merge child maps one at a time. Before merging, use sums at common keys to update ; then keep the larger value at each key. Checking first prevents connecting two states from the same child. The final map represents , so add to all entries using a shared offset.

Always merge the smaller map into the larger one. With balanced search trees, the time complexity per case is O(Nlog⁡2N)O(N\log^2 N)O(Nlog2N) and the space complexity is O(N)O(N)O(N). The answer is C1C_1C.

Proof

A completed path containing vvv uses zero, one, or two child edges. These choices make vvv a singleton, close a child path with endpoint value AvA_vAv​, or join two child paths with the same endpoint value. Their costs are respectively Bv+1B_v+1B, , and , giving the recurrence for .

Ou​(c)
Bv=∑uCuB_v=\sum_u C_uBv​=∑u​Cu​
max
(0, umax​Du​(Av​), u=w,cmax​(Du​(c)+Dw​(c)))
,
Cv​
=
Bv​+
1−
gv​.
0
c=Avc=A_vc=Av​
Mv​
(
c
)
.
v
​
:
0}
gvg_vgv​
MvM_vMv​
1−gv1-g_v1−gv​
1
​
v​
+
1
Bv+1−Du(Av)B_v+1-D_u(A_v)Bv​+1−Du​(Av​)
Bv+1−Du(c)−Dw(c)B_v+1-D_u(c)-D_w(c)Bv​+1−Du​(c)−Dw​(c)
CvC_vCv​

An open path ending at vvv uses at most one child, so Ov(c)=Bv−Mv(c)O_v(c)=B_v-M_v(c)Ov​(c)=Bv​−Mv​(c). Substituting this into Dv(c)=Cv−Ov(c)D_v(c)=C_v-O_v(c)Dv​(c)=Cv​−O gives the second recurrence. Every transition constructs a valid cover, proving the DP optimal by induction.

Keeping only the largest gain per key preserves the best candidate to join with a new child. Combinations of two previously processed children are already recorded in gvg_vgv​.

For a smaller map of size sss, charge its merge work to disappearing entries if at least half are duplicates. Otherwise, charge it to surviving entries, whose containing map grows to at least 3s/23s/23s/2. While an entry survives, its containing map never shrinks, and only one entry is initially created per vertex. Thus each entry receives O(log⁡N)O(\log N)O(logN) total charge, giving O(Nlog⁡N)O(N\log N)O(NlogN) entry visits.

v
​
(
c
)