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

Don't Mess with the Moles

Back to problem

Editorial

Say that www identifies uuu when a mole at www can locate uuu from its distance alone. On a path, one endpoint identifies every location. Otherwise, let FFF be the leaves and BBB the locations identified by no leaf. Deploying at F∪BF\cup BF∪B is optimal.

For each directed edge x→yx\to yx→y, let hx→yh_{x\to y}hx→y​ be the greatest distance from xxx within its component after deleting xyxy, and that component's diameter. A nonleaf is identified by a leaf exactly when some neighbor satisfies .

Compute these values by rerooting. Among branch heights 1+hz→x1+h_{z\to x}1+hz→x​ for neighbors z≠yz\neq yz=y and two zeros, take the largest two, . Then

hx→y=t1,Dx→y=max⁡(t1+t2,max⁡z≠yDz→x,0).h_{x\to y}=t_1,\qquad D_{x\to y}=\max\left(t_1+t_2,\max_{z\neq y}D_{z\to x},0\right).hx→y​=t1

Retaining the three largest branch heights and two largest branch diameters makes excluding one neighbor constant-time. Compute values from children to parents, then from parents to children. Select all leaves and nonleaves having no qualifying neighbor. Time and space complexity are O(N)O(N)O(N).

Proof

Suppose w≠uw\neq uw=u identifies uuu. Let aaa be the neighbor of uuu toward w, and the component containing after deleting . Then

x
y
Dx→yD_{x\to y}Dx→y​
uuu
aaa
ha→u=Da→uh_{a\to u}=D_{a\to u}ha→u​=Da→u​
t1,t2t_1,t_2
t1​,t2​
​
,
Dx→y​
=
max(t1​+t2​,z=ymax​Dz→x​,0).
w
w
CCC
aaa
uauaua
w identifies u  ⟺  max⁡x∈Cd(w,x)=d(w,a).w\text{ identifies }u\iff\max_{x\in C}d(w,x)=d(w,a).w identifies u⟺x∈Cmax​d(w,x)=d(w,a).

A farther location inside CCC would yield another location at distance d(w,u)d(w,u)d(w,u) along its path. Thus aaa is a diameter endpoint of CCC; the opposite endpoint is an original leaf that also identifies uuu. Conversely, if aaa is a diameter endpoint, the opposite endpoint identifies uuu. This gives the criterion ha→u=Da→uh_{a\to u}=D_{a\to u}ha→u​=Da→u​. It also shows that every location in BBB requires its own mole.

For each leaf, consider its path up to but excluding the first branching vertex. A mole outside this path cannot distinguish its last vertex from a vertex on another branch. Each such path therefore needs a mole. These paths are disjoint and identified by their respective leaves, hence disjoint from BBB. This proves the lower bound ∣F∣+∣B∣|F|+|B|∣F∣+∣B∣, attained by F∪BF\cup BF∪B.

The rerooting recurrence follows because a diameter stays within one branch or joins two through xxx. Its length is therefore the maximum of the branch diameters and the sum of the two greatest branch heights.