LeetCode-in-Java

2959. Number of Possible Sets of Closing Branches

Hard

There is a company with n branches across the country, some of which are connected by roads. Initially, all branches are reachable from each other by traveling some roads.

The company has realized that they are spending an excessive amount of time traveling between their branches. As a result, they have decided to close down some of these branches (possibly none). However, they want to ensure that the remaining branches have a distance of at most maxDistance from each other.

The distance between two branches is the minimum total traveled length needed to reach one branch from another.

You are given integers n, maxDistance, and a 0-indexed 2D array roads, where roads[i] = [ui, vi, wi] represents the undirected road between branches ui and vi with length wi.

Return the number of possible sets of closing branches, so that any branch has a distance of at most maxDistance from any other.

Note that, after closing a branch, the company will no longer have access to any roads connected to it.

Note that, multiple roads are allowed.

Example 1:

Input: n = 3, maxDistance = 5, roads = [[0,1,2],[1,2,10],[0,2,10]]

Output: 5

Explanation: The possible sets of closing branches are:

It can be proven, that there are only 5 possible sets of closing branches.

Example 2:

Input: n = 3, maxDistance = 5, roads = [[0,1,20],[0,1,10],[1,2,2],[0,2,2]]

Output: 7

Explanation: The possible sets of closing branches are:

It can be proven, that there are only 7 possible sets of closing branches.

Example 3:

Input: n = 1, maxDistance = 10, roads = []

Output: 2

Explanation: The possible sets of closing branches are:

It can be proven, that there are only 2 possible sets of closing branches.

Constraints:

Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
    private int get(int n, int maxDis, int mask, List<List<int[]>> al) {
        int nodes = 0;
        boolean[] m = new boolean[n];
        for (int i = 0; i < n; i++) {
            int val = mask & (1 << i);
            if (val > 0) {
                m[i] = true;
                nodes++;
            }
        }
        if (nodes == n) {
            return 1;
        }
        for (int startVertex = 0; startVertex < n; startVertex++) {
            if (m[startVertex]) {
                continue;
            }
            Queue<int[]> q = new LinkedList<>();
            q.add(new int[] {startVertex, 0});
            int[] dis = new int[n];
            Arrays.fill(dis, Integer.MAX_VALUE);
            dis[startVertex] = 0;
            int nodeCount = 1;
            while (!q.isEmpty()) {
                int[] curr = q.poll();
                for (int[] adj : al.get(curr[0])) {
                    if (!m[adj[0]] && curr[1] + adj[1] <= dis[adj[0]]) {
                        if (dis[adj[0]] == Integer.MAX_VALUE) {
                            nodeCount++;
                        }
                        dis[adj[0]] = curr[1] + adj[1];
                        q.add(new int[] {adj[0], dis[adj[0]]});
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                if (!m[i] && dis[i] > maxDis) {
                    return 0;
                }
            }
            if (nodes != n - nodeCount) {
                return 0;
            }
        }
        return 1;
    }

    private int solve(int n, int maxDis, List<List<int[]>> al) {
        int res = 0;
        for (int i = 0; i < (1 << n); i++) {
            res += get(n, maxDis, i, al);
        }
        return res;
    }

    public int numberOfSets(int n, int maxDistance, int[][] roads) {
        List<List<int[]>> al = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            al.add(new ArrayList<>());
        }
        for (int[] edge : roads) {
            al.get(edge[0]).add(new int[] {edge[1], edge[2]});
            al.get(edge[1]).add(new int[] {edge[0], edge[2]});
        }
        return solve(n, maxDistance, al);
    }
}