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

Rabbit

Back to problem

Editorial

Build the Kruskal reconstruction tree of the original graph. The maximum bottleneck value between two vertices is represented by the value of their LCA in this tree.

Fix the final vertex ttt. If a unit of mana at vertex vvv is collected as the kkk-th unit, then after collecting it the rabbit must still be able to travel from vvv to ttt while carrying kkk units. Thus each mana unit can be treated as a unit-time job with a deadline.

If there are ccc units with deadline ddd, the greedy transition is

x↦min⁡(d,x+c).x \mapsto \min(d,x+c).x↦min(d,x+c).

Equivalently, the answer is

min⁡D(D+the number of mana units with deadline greater than D).\min_D \left(D+\text{the number of mana units with deadline greater than }D\right).Dmin​(D+the number of mana units with deadline greater than D).

For a query edge (s,t,w)(s,t,w)(s,t,w), the two sides of the edge are connected while the threshold is below www. Therefore, on the Kruskal tree, we pull the two current components upward in decreasing order of edge values and update the answer candidates.

Processing the whole path for every query gives an O(QH)O(QH)O(QH) solution, where HHH is the height of the Kruskal tree. Under special condition B, the original graph is a path with random distinct capacities, so the Kruskal tree is a random Cartesian tree and has expected logarithmic height.

For the full solution, choose special vertices in the Kruskal tree so that every vertex is within O(N)O(\sqrt N)O(N​) edges from its nearest special ancestor. Each query is directly processed until one side reaches a special vertex.

For a fixed special vertex bbb, all remaining queries with one endpoint bbb are processed together. Precompute prefixes of type X operations on the chain from bbb to the root, and simulate all type Y operations on the other endpoints by DFS. This processes one special vertex in O(N)O(N)O(N) time. Since there are O(N)O(\sqrt N)O( special vertices, the total running time is .

N​
)
O(NN)O(N\sqrt N)O(NN​)