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

hyperopia

Back to problem

Editorial

Let ∅\varnothing∅ denote the forest with no edges. For a forest FFF and a vertex vvv, define F−vF-vF−v as the forest obtained by deleting every edge incident to vvv. The vertex itself is not deleted.

The information visible to the Ddas on vertex vvv is exactly F−vF-vF−v. In particular, there is exactly one possible forest in which their degree is 000: the forest F−vF-vF−v.

Let S(F)S(F)S(F) be the first day on which someone becomes certain in forest FFF, and define S(∅)=0S(\varnothing)=0S(∅)=0. Immediately after Dadas's announcement, the empty forest is eliminated from the possible worlds.

Suppose that the actual forest is FFF and everyone has declared uncertainty during the previous d−1d-1d−1 days. The Ddas on vertex vvv becomes certain on day ddd exactly when the degree-000 world F−vF-v is inconsistent with the previous declarations. Therefore,

S(F)=1+min⁡v:deg⁡F(v)>0S(F−v).S(F)=1+\min_{v:\deg_F(v)>0} S(F-v).S(F)=1+v:degF​

This recurrence is exactly the recurrence for the size of a minimum vertex cover.

Let τ(F)\tau(F)τ(F) be the size of a minimum vertex cover of FFF. If there are no edges, τ(F)=0\tau(F)=0τ(F)=0. If vertex vvv is selected, every edge incident to vv is covered, and the remaining problem is the minimum vertex cover of . Thus,

τ(F)=1+min⁡v:deg⁡F(v)>0τ(F−v).\tau(F)=1+\min_{v:\deg_F(v)>0} \tau(F-v).τ(F)=1+v:degF​

The two recurrences and their base cases are identical, so S(F)=τ(F)S(F)=\tau(F)S(F)=τ(F).

It remains to compute the minimum vertex cover size of the given forest. Root every tree at an arbitrary vertex. For the subtree of a vertex uuu, define:

  • dp0[u]\mathrm{dp}_0[u]dp0​[u]: the minimum vertex cover size when uuu is not selected
  • dp1[u]\mathrm{dp}_1[u]dp1​: the minimum vertex cover size when is selected

For every child www,

dp0[u]=∑wdp1[w],\mathrm{dp}_0[u]=\sum_w \mathrm{dp}_1[w],dp0​[u]=w∑​dp dp1[u]=1+∑wmin⁡(dp0[w],dp1[w]).\mathrm{dp}_1[u]=1+\sum_w \min(\mathrm{dp}_0[w],\mathrm{dp}_1[w]).dp1​[u]=1+w

For every connected component with root rrr, add min⁡(dp0[r],dp1[r])\min(\mathrm{dp}_0[r],\mathrm{dp}_1[r])min(dp0​[r],dp1​ to the answer.

A recursive DFS may overflow the call stack on a path of length 200 000200\,000200000. Build a traversal order iteratively and evaluate the DP in reverse order.

The time complexity is O(N+M)O(N+M)O(N+M), and the space complexity is O(N+M)O(N+M)O(N+M).

Solution written by GPT5.6

F−v
(
v
)
>
0
min
​
S
(
F
−
v).
v
F−vF-vF−v
(
v
)
>
0
min
​
τ
(
F
−
v).
[
u
]
uuu
1
​
[
w
]
,
∑
​
min
(
dp0​
[
w
]
,
dp1​
[
w
])
.
[
r
])