2477. Minimum Fuel Cost to Report to the Capital

```java
class Solution {
    long result = 0L;
    public long minimumFuelCost(int[][] roads, int seats) {
        List<List<Integer>> graph = buildGraph(roads);
        for (int next : graph.get(0)) {
            dfs(next, 0, graph, seats, new boolean[roads.length+1]);
        }
        return result;
    }
    private long dfs(int i, int parent, List<List<Integer>> graph, int seats, boolean[] visited) {
        long people = 1L;
        visited[i] = true;
        for (int next : graph.get(i)) {
            if (visited[next] || next == parent || next == 0) {
                continue;
            }
            people += dfs(next, i, graph, seats, visited);
        }
        result += (people/seats) + (people%seats > 0 ? 1 : 0);
    
        return people;
    }
    private List<List<Integer>> buildGraph(int[][] roads) {
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < roads.length+1; i++) {
            graph.add(new ArrayList<>());
        }
        for (int[] road : roads) {
            graph.get(road[0]).add(road[1]);
            graph.get(road[1]).add(road[0]);
        }
        return graph;
    }
}

/*
no circle, so no need visited
*/
```

Last updated