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

Blink Inspection

Back to problem

Editorial

For an incidence of vertex vvv and edge eee, let xv,ex_{v,e}xv,e​ be the parity of the number of edges incident to vvv that were inspected before eee. Edge e=uve=uve=uv incurs its cost exactly when

SuXORSvXORxu,eXORxv,e=1.S_u\mathbin{\mathrm{XOR}}S_v\mathbin{\mathrm{XOR}}x_{u,e}\mathbin{\mathrm{XOR}}x_{v,e}=1.Su​XORSv​

At a vertex of degree ddd, local ranks are 0,1,⋯ ,d−10,1,\cdots,d-10,1,⋯,d−1, so exactly ⌊d/2⌋\lfloor d/2\rfloor⌊d/2⌋ incidences have value 11. Conversely, any chosen incident edges can occupy the second, fourth, and later even positions.

For a tree, arbitrary local edge orders at all vertices can always be merged into one global order. If the union of all local precedence constraints contained a directed cycle, the line-graph structure of a tree would force that cycle to lie inside the clique belonging to one original vertex. That clique is oriented by a linear order and is acyclic, a contradiction.

The problem is therefore a static optimization: at every vertex vvv, choose exactly

qv=⌊deg⁡(v)2⌋q_v=\left\lfloor\frac{\deg(v)}2\right\rfloorqv​=⌊2deg(v)​⌋

incidences with value 111.

Root the tree. Let dp[v][p]\mathrm{dp}[v][p]dp[v][p] be the minimum subtree cost when the value on vvv's side of its parent edge is ppp. For a child www joined by edge eee, if the value on 's side is , define

Ge(a)=min⁡b∈{0,1}(dp[w][b]+Ce[SvXORSwXORaXORb=1]).G_e(a)=\min_{b\in\{0,1\}}\left(\mathrm{dp}[w][b]+C_e[S_v\mathbin{\mathrm{XOR}}S_w\mathbin{\mathrm{XOR}}a\mathbin{\mathrm{XOR}}b=1]\right).Ge​(a)=

Start with a=0a=0a=0 for every child. Then add the smallest qv−pq_v-pqv​−p values of Δe=Ge(1)−Ge(0)\Delta_e=G_e(1)-G_e(0).

Sorting the deltas at every vertex gives O(Nlog⁡N)O(N\log N)O(NlogN) time and O(N)O(N)O(N) memory.

Solution written by GPT5

XOR
xu,e​XOR
xv,e​=
1.
1
⌊d/2⌋\lfloor d/2\rfloor⌊d/2⌋
vv
v
aaa
b∈{0,1}min​(dp[w][b]+Ce​[Sv​XORSw​XORaXORb=1]).
Δe​
=
Ge​(1)−
Ge​(0)