Problem 637
Minimum Fuel Cost to Report to the Capital
Medium · 20 pts · Tree, Depth-First Search, Greedy
Problem
N cities form a tree of roads, and city 1 is the capital. Every other city sends one representative to the capital. Each city has one car with S seats; a representative can drive their own car or ride in another car, and cars can pick up passengers along the way. Driving a car along one road costs one litre of fuel. Find the minimum total fuel. Each road carries everyone from the subtree below it, so it needs ceil(subtree size / S) cars.
Input
The first line contains N and S. Each of the next N - 1 lines contains a road u v.
Output
Print the minimum litres of fuel.
Constraints
1 <= N <= 100000; 1 <= S <= 100000; the roads form a tree
Example input
4 5 1 2 1 3 1 4
Example output
3
Explanation
Cities 2, 3 and 4 each connect directly to the capital 1. Each representative drives their own car along one road, so with S = 5 the total is 1 + 1 + 1 = 3 litres.