BudiBadu Logo

Interstate Rescue Network Audit

Array Medium 6 views
Like19

Interstate Rescue Network Audit is a reachability count problem on an undirected road graph with selective closures. You receive the total depot count, a list of two-way highways, a starting depot, and a list of blocked highway references. Your task is to compute how many depots can still be reached from the start when those blocked routes are removed from traversal.

The reliable approach is graph traversal after filtering blocked links. Build adjacency using only open highways, then run BFS or DFS from the starting depot and count unique visited depots. Duplicate highways should not inflate counts, and disconnected components that are not connected to the origin must be ignored. If closures isolate the start, the answer is just the starting depot itself.

Judge behavior emphasizes correctness under partial shutdown conditions: chain networks cut in the middle, sparse graphs with separated islands, and cases where blocked routes leave only a tiny reachable component. Return a single integer count only. The core challenge is maintaining exact traversal constraints so every open depot is counted once and only once, while blocked links are treated as fully unavailable regardless of their direction in the original input list.

In implementation terms, treat closed-highway references consistently with the same indexing convention used by the input provider, then construct traversal edges only from allowed routes. This prevents accidental leakage through blocked links and keeps results aligned when route lists include repeated or symmetric entries.

Examples

Example 1
Input
n = 6, roads = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0, closed_highways = [[2,3]]
Output
3
Explanation

Depots 0, 1, and 2 receive the alert; the closure prevents reaching the rest.

Example 2
Input
n = 5, roads = [[0,1],[1,2],[2,3],[3,4]], start = 4, closed_highways = []
Output
5
Explanation

With no closures, the activation spreads to all depots.

Example 3
Input
n = 4, roads = [[0,1],[1,2],[2,3]], start = 2, closed_highways = [[1,2],[2,3]]
Output
1
Explanation

The origin depot is surrounded by closed highways, so only itself is active.

Algorithm Flow

Recommendation Algorithm Flow for Interstate Rescue Network Audit - Budibadu
Recommendation Algorithm Flow for Interstate Rescue Network Audit - Budibadu

Best Answers

java
import java.util.*;
class Solution {
    public int rescue_network_reach(int n, int[][] roads, int start, int[][] closed_highways) {
        Set<String> closed = new HashSet<>();
        for (int[] r : closed_highways) {
            int u = Math.min(r[0], r[1]);
            int v = Math.max(r[0], r[1]);
            closed.add(u + "-" + v);
        }

        Map<Integer, List<Integer>> adj = new HashMap<>();
        for (int[] r : roads) {
            int u = Math.min(r[0], r[1]);
            int v = Math.max(r[0], r[1]);
            if (!closed.contains(u + "-" + v)) {
                adj.computeIfAbsent(r[0], k -> new ArrayList<>()).add(r[1]);
                adj.computeIfAbsent(r[1], k -> new ArrayList<>()).add(r[0]);
            }
        }

        Set<Integer> visited = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        
        visited.add(start);
        queue.offer(start);
        
        while (!queue.isEmpty()) {
            int u = queue.poll();
            if(adj.containsKey(u)) {
                for (int v : adj.get(u)) {
                    if (visited.add(v)) {
                        queue.offer(v);
                    }
                }
            }
        }
        
        return visited.size();
    }
}