City Bus Loop Access
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
Stop 4 is closed, but the remaining stops 0, 1, 2, and 3 remain connected.
Stop 3 has no connecting roads, so the bus stays at its origin.
The bus visits stops 0, 1, and 2 before encountering the closed stop at 3.
Algorithm Flow

Best Answers
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();
}
}Related Tree Problems
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
