2477. Minimum Fuel Cost to Report to the Capital
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital
Python
import math
class Solution:
def minimumFuelCost(self, roads, seats):
self.fuel = 0
n = len(roads) + 1
mapper = [set() for i in range(n)]
for road in roads:
mapper[road[0]].add(road[1])
mapper[road[1]].add(road[0])
def dfs(node, parent,seats):
representatives = 1
for child in mapper[node]:
if child == parent:
continue
representatives += dfs(child, node, seats)
if node != 0:
self.fuel += math.ceil(representatives / seats)
return representatives
dfs(0, -1, seats)
return self.fuel