Practice · 1 of 3
Weighted Diameter
CHALLENGE
Difficulty: advanced+25 XP
**Bài toán.** A rooted tree (root 1); parents for v = 2..n on line 2, edge weights w[v] (edge parent(v)–v) on line 3. Print the diameter — the maximum total weight of any path. **Constraints:** 1 ≤ n ≤ 200 000; 1 ≤ w[v] ≤ 10^4; answer fits in long long. Post-order heights with the cross term h1 + h2 checked at EVERY node.
Press Submit to check your solution.
Back to lesson: Practice: Tree DP Trio