Skip to main content

Practice · 3 of 3

Sum of Distances from Every Node

CHALLENGE
Difficulty: advanced+25 XP

**Bài toán.** An unweighted tree (root 1), parents for v = 2..n. For every node v (in order 1..n) print the sum of distances from v to all other nodes. **Constraints:** 1 ≤ n ≤ 200 000; answers fit in long long. One bottom-up and one top-down pass: O(n).

Back to lesson: Practice: Tree DP Trio