BudiBadu Logo

City Bus Loop Access

Tree Medium 1 views
Like27

City Bus Loop Access models reachability in a transport network with temporary closures. You are given a graph of stops and roads, a starting stop, and a set of closed stations that cannot be visited or used as transit points. The task is to compute how many unique open stops are reachable from the start following valid open paths.

The natural solution is graph traversal (BFS or DFS) with a visited set. Build adjacency from the road list, skip closed stations entirely, and explore only open neighbors. If the start itself is closed, the correct answer is immediately zero. Otherwise, count each distinct reachable stop once.

The judge tends to include disconnected graphs, redundant edges, isolated starts, and closure patterns that split the network into smaller components. Your implementation should be deterministic and robust for these variations. Return only the final reachable-count integer; no path reconstruction is needed. In short, this is a clean connectivity-count problem where closure constraints are part of traversal validity, not a post-processing filter.

When the task involves connectivity or route cost, build adjacency carefully and guard against revisiting stale states. Use a visited or best-distance structure to avoid repeated work, and ensure unreachable scenarios return the required fallback value instead of partial traversal results.

Examples

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

Stop 4 is closed, but the remaining stops 0, 1, 2, and 3 remain connected.

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

Stop 3 has no connecting roads, so the bus stays at its origin.

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

The bus visits stops 0, 1, and 2 before encountering the closed stop at 3.

Algorithm Flow

Recommendation Algorithm Flow for City Bus Loop Access - Budibadu
Recommendation Algorithm Flow for City Bus Loop Access - Budibadu

Best Answers

java
import java.util.*;

class Solution {
    public int city_bus_loop_access(int n, int[][] roads, int start, int[] closed_stations) {
        Set<Integer> closed = new HashSet<>();
        for (int s : closed_stations) closed.add(s);
        if (closed.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) && !closed.contains(v)) {
                    visited.add(v);
                    queue.add(v);
                }
            }
        }
        return visited.size();
    }
}