There is a tree (i.e., a connected, undirected graph with no cycles) structure 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] = [a`

denotes that there exists a _{i}, b_{i}]**bidirectional road** connecting cities `a`

and _{i}`b`

._{i}

There is a meeting for the representatives of each city. 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 the car 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 to reach the capital city*.

**Example 1:**

Input:roads = [[0,1],[0,2],[0,3]], seats = 5Output:3Explanation:- Representative_{1}goes directly to the capital with 1 liter of fuel. - Representative_{2}goes directly to the capital with 1 liter of fuel. - Representative_{3}goes directly to the capital with 1 liter of fuel. It costs 3 liters of fuel at minimum. It can be proven that 3 is the minimum number of liters of fuel needed.

**Example 2:**

Input:roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2Output:7Explanation:- Representative_{2}goes directly to city 3 with 1 liter of fuel. - Representative_{2}and representative_{3}go together to city 1 with 1 liter of fuel. - Representative_{2}and representative_{3}go together to the capital with 1 liter of fuel. - Representative_{1}goes directly to the capital with 1 liter of fuel. - Representative_{5}goes directly to the capital with 1 liter of fuel. - Representative_{6}goes directly to city 4 with 1 liter of fuel. - Representative_{4}and representative_{6}go together to the capital with 1 liter of fuel. It costs 7 liters of fuel at minimum. It can be proven that 7 is the minimum number of liters of fuel needed.

**Example 3:**

Input:roads = [], seats = 1Output:0Explanation:No representatives need to travel to the capital city.

**Constraints:**

`1 <= n <= 10`

^{5}`roads.length == n - 1`

`roads[i].length == 2`

`0 <= a`

_{i}, b_{i}< n`a`

_{i}!= b_{i}`roads`

represents a valid tree.`1 <= seats <= 10`

^{5}

**Solution: Greedy + DFS**

To reach the minimum cost, we must share cars if possible, say X reps from children nodes to an intermediate node u on the way towards capital 0. Then they all changes cars at node u, and we need (X + 1) // seats cars/fuel from u to 0.

We use DFS to count # of reps at each node u while accumulating the total cost.

Time complexity: O(n)

Space complexity: O(n)

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Author: Huahua class Solution { public: long long minimumFuelCost(vector<vector<int>>& roads, int seats) { long long ans = 0; vector<vector<int>> g(roads.size() + 1); for (const vector<int>& r : roads) { g[r[0]].push_back(r[1]); g[r[1]].push_back(r[0]); } // Returns total # of children of u. function<int(int, int)> dfs = [&](int u, int p, int rep = 1) { for (int v : g[u]) if (v != p) rep += dfs(v, u); if (u) ans += (rep + seats - 1) / seats; return rep; }; dfs(0, -1); return ans; } }; |