Technique
Centroid Decomposition
Recursively remove a centroid (largest remaining part <= n/2) so every path passes through some centroid at one of O(log n) levels.
0%
mastery
0/4 solved
0/4 solved
Recognise it
The signal
count/optimise over ALL paths in a tree (length k, weight bound)
1Brute force
All pairs of nodes O(n2).
2The observation
Pair enumeration: each path is handled at the unique highest centroid on it.
3Why it works
The centroid tree has depth O(log n) and each path is counted at its top centroid.
Reach for it when
- +nearest marked node queries with updates
Not when
- ×path queries between given endpoints (binary-lifting/hld simpler)
- ×dynamic tree structure
Practice
0/4 solved
- Fixed-Length Paths ICSESHard
- Fixed-Length Paths IICSESHard
- Xenia and TreeCodeforces2400
- Finding a CentroidCSESHard
Pitfalls
Misconceptions
- !double counting paths that stay in one child subtree
Go deeper1 combinations · resources
Combinations
Centroid Decomposition+Graph BFS / DFS
count paths of length k; nearest marked vertex queries
- Centroid Decomposition:
- splits paths by their top centroid
- Graph BFS / DFS:
- BFS/DFS collects depths from each centroid