You are given a tree with nodes and an integer . Count the number of paths with exactly edges. A path is any sequence of edges connecting two nodes. A brute-force approach checking all paths is too slow for large .
Centroid decomposition reduces this to by processing paths through centroids. Let me show you how to split this problem into manageable pieces using centroid decomposition.
Space complexity is for the data structures used.