Minimum Fuel Cost to Report to the Capital

There is a tree, i.e. a connected, undirected graph with no cycles, representing a country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads. The capital city is city 0.

You are given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.

There is a meeting for the representatives of each city, and the meeting is in the capital city.

There is a car in each city. You are given an integer seats that indicates the number of seats in each car.

A representative can use the car in their city to travel, or change cars and ride with another representative. The cost of traveling between two cities is one liter of fuel.

Return the minimum number of liters of fuel needed for all representatives to reach the capital city.

Example 1
Inputroads = [[0,1],[0,2],[0,3]], seats = 5
Output3
Each of the three non-capital representatives travels directly to the capital, costing 1 liter each for a minimum total of 3 liters.
Example 2
Inputroads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
Output7
Representatives can share cars along the tree so that the total minimum fuel cost to reach the capital is 7 liters.

Constraints

  • 1 <= n <= 10^5
  • roads.length == n - 1
  • roads[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • roads represents a valid tree.
  • 1 <= seats <= 10^5

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate