Graph Theory37 sections · 1633 units
Open in Course

Think First - Fixed-Length Paths I

(Counting paths by length)

You need to count paths with a specific length k. This is not a subtree query (not asking "all nodes under v"). It is not a path value query (not asking "sum from u to v"). It is a counting problem. Naive approach: for every pair of nodes, compute the distance using LCA and check if it equals k. That is O(n2)O(n^2) pairs times O(logn)O(\log n) per LCA, way too slow for 200200,000000 nodes. Can you split the tree into smaller pieces and count paths within each piece?

Think about recursive decomposition. What property would the split point need?