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

yet another mst problem

Back to problem

Editorial

1. Candidate positions of the moved point

Suppose point rrr is moved. Let QQQ be the set of the other points, and let sss be the point after the move.

Fix any spanning tree in an optimal solution. If AAA is the set of vertices adjacent to sss, the part of its weight that depends on the position of sss is

∑i∈A∣xs−xi∣+∑i∈A∣ys−yi∣.\sum_{i \in A} |x_s-x_i| + \sum_{i \in A} |y_s-y_i|.i∈A∑​∣xs​−x

The two sums are independent. A sum of absolute differences is minimized at a median, so an optimal xsx_sxs​ can be chosen as the xxx-coordinate of a point in AAA, and an optimal ysy_sys can be chosen as the -coordinate of a point in .

Therefore, it is sufficient to try every combination of an xxx-coordinate and a yyy-coordinate appearing in QQQ. There are at most (N−1)2(N-1)^2(N−1)2 candidate positions. If , the answer is .

2. Keeping only one MST of the remaining points

Let GGG be the complete Manhattan graph on QQQ, and let KKK be any MST of GGG. After adding a candidate point sss, there is one new edge from sss to every point of .

It is sufficient to keep only the edges of KKK and these new edges. Consider an MST of the augmented graph that uses as few old edges outside KKK as possible. Suppose it contains such an edge eee. On the path between the endpoints of eee in KKK, every edge has weight at most the weight of eee. Removing from the chosen MST creates a cut, and the path in contains an edge crossing this cut. Replacing with does not increase the weight and decreases the number of old edges outside , which is a contradiction.

Thus, for each candidate position, we only need to run Kruskal's algorithm on a graph with ∣Q∣+1|Q|+1∣Q∣+1 vertices and 2∣Q∣−12|Q|-12∣Q∣−1 edges.

3. Sorting the new edges in linear time

The edges of KKK do not change while rrr is fixed, so they are sorted once. Let the candidate position be (X,Y)(X,Y)(X,Y). Partition the points of QQQ into four regions.

  • xi≤Xx_i \leq Xxi​≤X, yi≤Yy_i \leq Yyi​≤Y: the distance is , so the order is decreasing .

Pre-sort the points by these four keys. For one candidate position, filtering each order by its corresponding region produces four sorted streams of edges incident to sss. Merge these streams together with the sorted edges of KKK while running Kruskal's algorithm. This takes O(N)O(N)O(N) time per candidate.

4. Complete algorithm

For every choice of the moved point rrr:

  • Build an MST KKK of the complete Manhattan graph on QQQ using Prim's algorithm.
  • Try every combination of a distinct xxx-coordinate and a distinct yyy-coordinate appearing in QQQ.
  • For each candidate, merge the edges of KKK and the four sorted streams of edges incident to , and run Kruskal's algorithm.

The first observation guarantees that some optimal moved position is considered. The second observation guarantees that the MST weight for every considered position is computed exactly. Since every original point is considered as the moved point, the minimum found by the algorithm is the answer.

There are O(N2)O(N^2)O(N2) candidate positions for each moved point, and each candidate is processed in O(N)O(N)O(N) time. Over all choices of the moved point, the time complexity is O(N4)O(N^4)O(N and the memory complexity is .

Solution written by GPT5.6

i​
∣
+
i∈A∑​∣ys​−
yi​∣.
​
yyy
AAA
N=1N=1
N=1
000
QQ
Q
ee
e
KKK
fff
eee
fff
KKK
X+Y−(xi+yi)X+Y-(x_i+y_i)
X+Y−(xi​+yi​)
xi+yix_i+y_ixi​+yi​
  • xi≤Xx_i \leq Xxi​≤X, yi>Yy_i > Yyi​>Y: the distance is X−Y+(yi−xi)X-Y+(y_i-x_i)X−Y+(yi​−xi​), so the order is increasing yi−xiy_i-x_iyi​−xi​.
  • xi>Xx_i > Xxi​>X, yi≤Yy_i \leq Yyi​≤Y: the distance is −X+Y+(xi−yi)-X+Y+(x_i-y_i)−X+Y+(xi​−yi​), so the order is increasing xi−yix_i-y_ixi​−yi​.
  • xi>Xx_i > Xxi​>X, yi>Yy_i > Yyi​>Y: the distance is −X−Y+(xi+yi)-X-Y+(x_i+y_i)−X−Y+(xi​+yi​), so the order is increasing xi+yix_i+y_ixi​+yi​.
  • sss
    4
    )
    O(N2)O(N^2)O(N2)