Centroid Decomposition

You know tree DP. Now divide trees by centroids. Solve path-counting problems where brute force fails and centroid decomposition runs in O(n log n).

30 lessons
142 min
Codeforces: 1900-2400LeetCode: 2000-2300

Lessons

1. Introduction to Centroid Decomposition

Divide and conquer on trees

4m

2. What Centroid Decomposition Solves

Path counting, nearest queries

4m

3. When to Use Centroid Decomposition

All paths pass through some centroid

3m

4. Centroid Decomposition Template

The decomposition template

4m

5. When Centroid Decomposition Helps

Problem type patterns

4m

6. Problem - IOI Race

IOI 2011 - min edges for weight K

3m1 problems

7. IOI Race - Why Naive Fails

Optimize over paths through centroid

4m

8. IOI Race - Defining the DP

best[w] = min edges for weight w

4m

9. IOI Race - Implementation

Clear best after each centroid

5m1 problems

10. IOI Race - Time and Space

O(n log n) with proper cleanup

3m

11. Problem - Distance in Tree

CF 161D - pairs at distance k

4m1 problems

12. Distance in Tree - Why Naive Fails

Each pair passes through one centroid

4m

13. Distance in Tree - Defining the DP

Count pairs, not paths

4m

14. Distance in Tree - Core Logic

Merge subtree counts carefully

4m

15. Distance in Tree - Implementation

cnt[d] tracks seen distances

5m

16. Problem - Xenia and Tree

CF 342E - nearest red node queries

4m1 problems

17. Xenia and Tree - Why Naive Fails

Centroid ancestors store min distances

4m

18. Xenia and Tree - Defining the DP

O(log n) ancestors per node

4m

19. Xenia and Tree - Core Logic

Update all ancestors on paint

4m

20. Xenia and Tree - Implementation

Query all ancestors, take min

5m

21. Problem - Close Vertices

CF 293E - distance ≤ l, weight ≤ w

3m1 problems

22. Close Vertices - Solution

Sort by distance, two-pointer + BIT

4m1 problems

23. Quiz: Centroid Properties

What makes a centroid?

3m1 problems

24. Quiz: Counting Paths

Subtree merging patterns

3m1 problems

25. Common Mistakes in Centroid Decomposition

Forgot to remove, no reset

4m

26. Problem - Digit Tree

CF 715C - digit paths

10m1 problems

27. Digit Tree - Solution

The solution

10m

28. Problem - Query on a Tree V

SPOJ QTREE5 - colored queries

10m1 problems

29. Query on a Tree V - Solution

The solution

10m

30. Section Recap

Split at centroid, process recursively

5m

Ready to start learning?

Access all 30 lessons with interactive content and progress tracking.