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

Reserve Maintenance Team

Back to problem

Editorial

Consider the induced subgraph containing only stations whose teams have already been assigned. Since the original graph is a tree, its connected components can be maintained with a disjoint-set union structure. A path P(u,v)P(u,v)P(u,v) has no waiting team exactly when uuu and vvv belong to the same assigned-only component.

Represent every registered path by a forbidden edge between the current components of its endpoints. Assigning station xxx merges xxx with all already assigned neighboring components. A forbidden edge inside the set being merged means an old path would become empty. The new path is checked in the same way after the hypothetical merge.

Store the neighboring forbidden components for each component and merge smaller sets into larger sets. Testing plan 000 before plan 111 enforces the required priority.

Solution written by GPT5