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

Moving an Edge

Back to problem

Editorial

By the standard endpoint-normalization and long-move exchange lemmas, an optimum exists in one of the following forms.

  • The deleted and added edges share one endpoint uuu.
  • If uuu is not a leaf, the old and new attachment vertices are at distance at most 222.
  • If uuu is a leaf, the new attachment vertex may be arbitrarily far away.

Delete uv1uv_1uv1​ and add uv2uv_2uv2​. Let AAA be the component containing uuu after the deletion, and let ∣A∣=a|A|=a∣A∣=a. Distances inside either component do not change. For every x∈Ax\in Ax∈A and every vertex yyy in the other component, the distance changes exactly when

dist⁡(v1,y)≠dist⁡(v2,y).\operatorname{dist}(v_1,y)\ne\operatorname{dist}(v_2,y).dist(v1​,y)=dist(v

If dist⁡(v1,v2)\operatorname{dist}(v_1,v_2)dist(v1​,v2​) is odd, every such yyy changes. If it is even, only the two path-direction branches at the midpoint contribute.

For distance 111, the cost is a(N−a)a(N-a)a(N−a). For distance 222, along the path u−v1−m−v2u-v_1-m-v_2u−v, let be the size of the side of , let be the size of the branch at , and let be the size of the branch at . The cost is

a(S−a+b).a(S-a+b).a(S−a+b).

Both expressions are concave in aaa, so only the minimum and maximum available branch sizes are needed. Keeping the two smallest and two largest branch sizes at every vertex handles all distance-111 and distance-222 candidates in linear time.

For a leaf move with an even old-to-new attachment distance, let mmm be the midpoint. Let XXX be the branch containing the moved leaf and YYY the branch containing the new attachment vertex. The cost is

sX+sY−1.s_X+s_Y-1.sX​+sY​−1.

For every directed branch from a center vertex, compute its size sss, maximum depth hhh, and nearest original-leaf distance ℓ\ellℓ. Branch XXX is usable when ℓ≥2\ell\ge2ℓ≥2, and another branch YY is usable when

hY≥ℓX−1.h_Y\ge\ell_X-1.hY​≥ℓX​−1.

Thus each branch asks for the minimum size of another branch whose height is at least a threshold.

All directed-branch values are computed with rerooting DP. Sort branch records by center and decreasing height, and sort queries by center and decreasing threshold. Since every key is an integer between 000 and NNN, stable counting sort makes both orderings linear. During the sweep, maintain the two smallest active branch sizes so that the query branch itself can be excluded.

The total time complexity is O(N)O(N)O(N) and the memory complexity is O(N)O(N)O(N).

Solution written by GPT5.6

2
​
,
y
)
.
1
​
−
m−
v2​
SSS
v1v_1v1​
v1mv_1mv1​m
aaa
uuu
v1v_1v1​
bbb
v2v_2v2​
mmm
Y