Graph Theory37 sections · 1633 units
Open in Course

Problem - Fixed-Length Paths I

(CSES 2080)

You are given a tree with nn nodes and an integer kk. Count the number of paths with exactly kk edges. A path is any sequence of edges connecting two nodes. A brute-force approach checking all O(n2)O(n^2) paths is too slow for large nn.

Centroid decomposition reduces this to O(nlogn)O(n \log n) by processing paths through centroids. Let me show you how to split this problem into manageable pieces using centroid decomposition.

Space complexity is O(n)O(n) for the data structures used.