Distinct Colors
CodeChefOpen on judge ↗
You are given a tree with N vertices rooted at 1. You consider all the N subtrees (rooted at 1,2..N such that subtree rooted at i consists of i and all nodes which contain i as an ancestor). Each node in the graph also consists of a color. The colors can be between 1 to K. For each subtree find the no. of distinct colours in that subtree. Output the sum of all these N numbers. Input - First
HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17
CodeSearch Tutor
Hints, not spoilers — it won’t hand over the full solution unless you insist.
Sign in to chat with the tutor and save your progress.
Sign in to start