DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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

Identity

Back to problem

Editorial

For distinct vertices u,vu,vu,v, define

δ(u,v)=max⁡(1,dT(u,v)).\delta(u,v)=\max(1,d_T(u,v)).δ(u,v)=max(1,dT​(u,v)).

This function satisfies the triangle inequality.

Suppose the vertices occur from left to right in the order p1,p2,…,pNp_1,p_2,\ldots,p_Np1​,p2​,…,pN​. Every consecutive coordinate gap is at least . Conversely, setting the consecutive gaps equal to these values satisfies every pairwise constraint by the triangle inequality. The problem is therefore the shortest Hamiltonian path problem in the metric .

Contract all edges of weight 000. The resulting graph HHH is a tree whose edges all have weight 111. Let cvc_vcv​ be the number of original vertices represented by a vertex vvv of , and let be its degree in .

Expand every transition between different components into its path in HHH. This gives a walk covering HHH. If its first and last components are sss and ttt, let PPP be their path. Every edge on PPP is traversed at least once, and every other edge at least twice.

Two extra traversals of an edge cost 222 and add at most one usable visit at each endpoint. Each additional original vertex placed during the same visit also costs 111, so extra edge traversals cannot improve the answer. It is sufficient to traverse every edge on PPP once and every other edge twice.

In such a walk, a component vvv is visited deg⁡(v)−1\deg(v)-1deg(v)−1 times if it is an internal vertex of PPP, and deg⁡(v)\deg(v)deg(v) times otherwise. Original vertices exceeding this number cost one each because they must be placed consecutively within their component.

Define

B=2(∣V(H)∣−1)+∑v∈V(H)max⁡(0,cv−deg⁡(v)).B=2(|V(H)|-1)+\sum_{v\in V(H)}\max(0,c_v-\deg(v)).B=2(∣V(H)∣−1)+v

Making vvv an internal vertex of PPP adds one to the cost exactly when cv≥deg⁡(v)c_v\geq\deg(v)cv​≥deg(v). Call such a vertex marked. The answer for a path PP is

B−(∣E(P)∣−#{marked internal vertices of P}).B-\left(|E(P)|-\#\{\text{marked internal vertices of }P\}\right).B−(∣E(P)∣−#{marked internal vertices of P}).

Thus, maximize the parenthesized benefit.

Root HHH arbitrarily. Let downvdown_vdownv​ be the maximum benefit of a path having vvv as one endpoint and going into the subtree of vvv. An arm through a child has value

1+max⁡(0,downu−[u is marked]).1+\max(0,down_u-[u\text{ is marked}]).1+max(0,downu​−[u is marked]).

Keeping the two largest child arms checks both paths ending at vvv and paths passing through vvv. Process an iterative DFS order in reverse to avoid recursion depth issues.

If HHH has only one vertex, all pairwise distances are 111, so the answer is N−1N-1N−1.

The time complexity is O(Nα(N))O(N\alpha(N))O(Nα(N)), and the space complexity is O(N)O(N)O(N).

In subtask 1, if the tree diameter is DDD, the answer is 2(N−1)−D2(N-1)-D2(N−1)−D. In subtask 2, enumerate the starting vertex and traverse the contracted tree to evaluate every path benefit in O(N2)O(N^2)O(N2 time.

δ(pi,pi+1)\delta(p_i,p_{i+1})
δ(pi​,pi+1​)
δ\deltaδ
HHH
deg⁡(v)\deg(v)deg(v)
HHH
∈
V
(
H
)
∑
​
max
(
0
,
cv​
−
deg(v)).
P
uu
u
)