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

Colorful Ancestors

Back to problem

Editorial

Consider the contribution of each color independently.

Fix a color ccc. Every vertex vvv has a value Tc(v)T_c(v)Tc​(v), which is either 000 or a vertex of color ccc. The contribution of color ccc to the answer is the number of ordered pairs with equal TcT_cTc​ values, namely

∑xcntc(x)2\sum_x cnt_c(x)^2x∑​cntc​(x)2

where cntc(x)cnt_c(x)cntc​(x) is the number of vertices vvv such that Tc(v)=xT_c(v)=xTc​. Here, is either or a vertex of color .

Now consider a vertex xxx of color ccc. A vertex vvv satisfies Tc(v)=xT_c(v)=xTc​(v)=x if and only if

  • vvv lies in the subtree of xxx.
  • On the path from xxx down to vvv, excluding xxx itself, there is no vertex of color ccc.

So we start with all vertices in the subtree of xxx, then subtract the subtrees of the closest same-color descendants of xxx.

Thus,

cntc(x)=subtree(x)−∑subtree(y),cnt_c(x)=subtree(x)-\sum subtree(y),cntc​(x)=subtree(x)−∑s

where the sum is over vertices yyy such that xxx is the closest same-color ancestor of yyy.

It remains to compute, for every vertex yyy, its closest same-color ancestor sameParent(y)sameParent(y)sameParent(y). This can be done in one DFS from the root. During DFS, maintain last[c]last[c]last[c], the deepest vertex of color cc on the current root-to-current path.

When entering a vertex vvv, the current value of last[Cv]last[C_v]last[Cv​] is exactly sameParent(v)sameParent(v)samePar. Store it, then set . When leaving , roll it back. Each vertex is processed once, so this is linear.

We also need the number of vertices with Tc(v)=0T_c(v)=0Tc​(v)=0. For a fixed color ccc, consider vertices of color ccc that have no same-color ancestor. Their subtrees are disjoint, and every vertex inside them has some color-cc ancestor. Therefore,

cntc(0)=N−∑subtree(x),cnt_c(0)=N-\sum subtree(x),cntc​(0)=N−∑subtree(x),

where the sum is over color-ccc vertices xxx with no same-color ancestor.

In the implementation:

  • If sameParent(v)=0sameParent(v)=0sameParent(v)=0, add subtree(v)subtree(v)subtree(v) to rootSum[.

Then the answer is

∑c=1K(N−rootSum[c])2+∑v=1N(subtree(v)−sameChildSum[v])2.\sum_{c=1}^{K}(N-rootSum[c])^2+ \sum_{v=1}^{N}(subtree(v)-sameChildSum[v])^2.c=1∑K​(

Since NNN can be 2 000 0002\,000\,0002000000, a recursive DFS may cause stack overflow. An iterative DFS is safer.

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

(
v
)
=
x
xxx
000
ccc
u
b
t
r
ee
(
y
)
,
c
e
n
t
(
v
)
last[Cv]=vlast[C_v]=vlast[Cv​]=v
vvv
c
Cv]rootSum[C_v]
rootSum[Cv​]
  • Otherwise, add subtree(v)subtree(v)subtree(v) to sameChildSum[sameParent(v)]sameChildSum[sameParent(v)]sameChildSum[sameParent(v)].
  • N
    −
    rootSum[c])2+
    v=1∑N​(subtree(v)−
    sameChildSum[v])2.