Jason PDF
Jason PDF
Jason Chiu
1 The Problem
Problem description
Solution
Proof of correctness
Time complexity
2 Centroid Decomposition
3 Example Problem
The solution of the previous problem finds a node v which we shall call a
centroid of the tree. Now what happens if we apply the algorithm
recursively to each subtree split by the centroid?
Given a weighted tree with N nodes, find the minimum number of edges in
a path of length K , or return −1 if such a path does not exist.
1 ≤ N ≤ 200000
1 ≤ length(i, j) ≤ 1000000 (integer weights)
1 ≤ K ≤ 1000000
Better solution:
Perform centroid decomposition to get a “tree of subtrees”
Start at the root of the decomposition, solve the problem for each
subtree as follows
Solve the problem for each “child tree” of the current subtree
Perform DFS from the centroid on the current subtree to compute
the minimum edge count for paths that include the centroid
Two cases: centroid at the end or in the middle of path
Use a timestamped array of size 1000000 to keep track of which
distances from centroid are possible and the minimum edge count for
that distance
Take the minimum of the above two
Time complexity: O(n log n)