← All problemsSign in

Madoka and Ladder Decomposition

CodeChefRating 3050Open on judge ↗

Madoka was given a tree on her coming of age, and not a simple one, but a rooted tree of $n$ vertices with a root at the vertex with the number $1$. For all $i\ge 2$, let $P_i$ ($1 \leq P_i \leq i - 1$) be the parent of the vertex $i$. Let's define the depth array $h$ as follows: $h_1 = 1$, and $h_i = h_{P_i} + 1$ for all $i \ge 2$. The subtree of a vertex $u$, denoted $S(u)$, is defined as the

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