What Centroid Decomposition Solves

Path counting, nearest queries

Centroid decomposition handles problems involving paths in trees. Count paths of a given length. Find paths with specific properties. Answer queries about distances or path values.

Here's the main idea: every path passes through some centroid in the decomposition. By processing paths through each centroid, you cover all paths exactly once. You'll see why this matters in the problems.

unnamed - 2026-01-20T020016.805.jpg