← All problemsSign in

Heavy-Light Decomposition

CodeChefRating 2791Open on judge ↗

You are given a rooted tree with N nodes. The nodes are numbered from 1 to N, and the root is the node numbered 1. For each node X which is not a leaf, you must choose a node Y which is a child of X and mark the edge between X and Y as heavy (red in the picture below). Once this has been done, we calculate the cost of each path from the root to a leaf. For this, we decompose the path into segment

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.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start