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

Tree Operation

Back to problem

Editorial

If N=1N=1N=1, performing the operation on the only vertex makes its value 111. Therefore, the answer is 111.

Now assume N≥2N \ge 2N≥2.

First, no vertex can have final value at least 333. For a vertex vvv to become at least 333, every neighbor of vvv must have value at least 222 right before the operation on vvv. However, if a neighbor of was operated before and became at least , then at that time its neighbor should already have value at least . But had not been operated yet, so its value was still . This is a contradiction. Thus, final values can only be .

For N≥2N \ge 2N≥2, we can make every vertex at least 111. For example, perform the operation on every vertex in any order. Since the minimum adjacent value is always at least 000, each operated vertex becomes at least 111.

Now consider the vertices whose final value is 222. No two adjacent vertices can both have value 222. Suppose adjacent vertices uuu and vvv both become 222. Let uuu be the one operated earlier. When u was operated, had not been operated yet, so its value was . Therefore, could not become , a contradiction.

So the set of vertices with value 222 must be an independent set.

Conversely, for any independent set SSS in the tree, we can make exactly those vertices have value 222. First, perform the operation on all vertices not in SSS. Their values become at least 111. Then perform the operation on all vertices in SSS. Since SSS is independent, every neighbor of each vertex in lies outside and already has value at least . Thus, every vertex in becomes .

Therefore, for N≥2N \ge 2N≥2, the maximum sum is

N+α,N + \alpha,N+α,

where α\alphaα is the size of a maximum independent set of the tree.

The maximum independent set of a tree can be computed by dynamic programming. Root the tree at vertex 111 and define:

  • dp0[v]dp0[v]dp0[v]: the maximum number of chosen vertices in the subtree of vvv when vvv is not chosen
  • dp1[v]dp1[v]dp1[v]: the maximum number of chosen vertices in the subtree of vv when is chosen

Then:

dp1[v]=1+∑u child of vdp0[u],dp1[v] = 1 + \sum_{u \text{ child of } v} dp0[u],dp1[v]=1+u child of v∑​dp0[u], dp0[v]=∑u child of vmax⁡(dp0[u],dp1[u]).dp0[v] = \sum_{u \text{ child of } v} \max(dp0[u], dp1[u]).dp0[v]=u child of v∑​max(dp0[u],

The maximum independent set size is max⁡(dp0[1],dp1[1])\max(dp0[1], dp1[1])max(dp0[1],dp1[1]), and the answer is N+max⁡(dp0[1],dp1[1])N+\max(dp0[1],dp1[1])N+max(dp0[1],.

The time complexity is O(N)O(N)O(N), and the memory usage is O(N)O(N)O(N).

uu
u
vvv
vvv
222
vvv
111
vvv
000
0,1,20,1,20,1,2
u
u
vvv
000
uuu
222
SS
S
SSS
111
SSS
222
v
vvv
d
p
1
[
u
])
.
d
p
1
[
1
])