Find the centroid . Solve the problem for paths passing through . Remove and recursively solve for each component. Combine the results. This is the standard pattern for all centroid decomposition problems. The recursion depth is , and processing paths through each centroid takes or depending on the problem.
The total time depends on what you do at each centroid. This pattern applies to path counting, distance queries, and weighted path problems. You will see it repeatedly in the problems below.
Space complexity is for the data structures used.