BudiBadu Logo

Disaster Relief Route Tracker

Array Medium 0 views
Like17

Disaster Relief Route Tracker is a connectivity count problem with blocked shelters. You get the number of shelters, undirected roads, a start shelter, and a list of blocked shelter IDs. Count how many open shelters are reachable from the start without passing through blocked shelters.

Build graph adjacency, mark blocked shelters, and run BFS or DFS while skipping blocked nodes. If the start is blocked, reachability is zero in a strict interpretation; in this dataset the start is open in tested cases, and traversal should count each reachable open shelter once. Duplicate roads should not create duplicate visits, so maintain a visited set throughout the traversal.

Judge scenarios include partially blocked graphs, disconnected components, and cases where one blocked hub sharply reduces coverage. Return only the integer count. The important implementation detail is node-level blocking (shelters), not edge-level blocking, so checks happen before visiting nodes and when iterating neighbors. With clean filtering and one traversal pass, the result stays deterministic and consistent across sparse and dense road maps.

Keep blocked-shelter checks on both queue insertion and neighbor expansion so blocked nodes are never traversed indirectly. This ensures computed coverage matches operational rules where closed shelters cannot relay routes, even if they still appear in adjacency lists from raw road data.

Examples

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

The convoy reaches shelters 1, 0, 2, 3, and 5 while skipping the blocked shelter 4.

Example 2
Input
n = 4, roads = [], start = 2, blocked_shelters = []
Output
1
Explanation

With no roads, only the staging shelter receives supplies.

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

The convoy can only deliver to shelters 0 and 1 before encountering a blocked location.

Algorithm Flow

Recommendation Algorithm Flow for Disaster Relief Route Tracker - Budibadu
Recommendation Algorithm Flow for Disaster Relief Route Tracker - Budibadu

Best Answers

java
import java.util.*;

class Solution {
    public int disaster_relief_route_tracker(int n, int[][] roads, int start, int[] blocked_shelters) {
        Set<Integer> blocked = new HashSet<>();
        for (int s : blocked_shelters) blocked.add(s);
        if (blocked.contains(start)) return 0;
        List<Integer>[] adj = new ArrayList[n];
        for (int i = 0; i < n; i++) adj[i] = new ArrayList<>();
        for (int[] road : roads) {
            adj[road[0]].add(road[1]);
            adj[road[1]].add(road[0]);
        }
        Set<Integer> visited = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        visited.add(start);
        queue.add(start);
        while (!queue.isEmpty()) {
            int u = queue.poll();
            for (int v : adj[u]) {
                if (!visited.contains(v) && !blocked.contains(v)) {
                    visited.add(v);
                    queue.add(v);
                }
            }
        }
        return visited.size();
    }
}