BudiBadu Logo
00:00

City Bus Loop Access

Graph Easy 1 views

City transit planners monitor a circular bus system where stops are labelled from 0 to n - 1 and connected by two-way service roads. During street fairs, maintenance, or weather events, specific stops may close for several hours, preventing buses from entering those locations or using them to reach others in the loop. When dispatch wants to estimate midday coverage from a particular origin stop, they need a quick way to count how many distinct stops remain viable along open roads.

Your task is to implement a function that receives the total number of stops n, a list of two-way roads roads, the starting stop start, and a set closed_stations identifying every stop that is unavailable. Each road is represented as [u, v] and may appear multiple times in the list due to separate inspection reports. Treat duplicates as a single connection. The bus must avoid closed stops entirely, including as pass-through points, and should only account for unique stops reached via the open network.

Return the number of accessible stops that the bus can reach from start, counting the starting stop itself if it is open. If start belongs to closed_stations, the dispatcher considers the coverage to be zero. Roads that lead to closed stations should be ignored, even if alternate routes exist. Provide the final answer as an integer.

Example 1:

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.

Example 2:

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 3:

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.

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.

BudiBadu Logo

City Bus Loop Access

Graph Easy 1 views

City transit planners monitor a circular bus system where stops are labelled from 0 to n - 1 and connected by two-way service roads. During street fairs, maintenance, or weather events, specific stops may close for several hours, preventing buses from entering those locations or using them to reach others in the loop. When dispatch wants to estimate midday coverage from a particular origin stop, they need a quick way to count how many distinct stops remain viable along open roads.

Your task is to implement a function that receives the total number of stops n, a list of two-way roads roads, the starting stop start, and a set closed_stations identifying every stop that is unavailable. Each road is represented as [u, v] and may appear multiple times in the list due to separate inspection reports. Treat duplicates as a single connection. The bus must avoid closed stops entirely, including as pass-through points, and should only account for unique stops reached via the open network.

Return the number of accessible stops that the bus can reach from start, counting the starting stop itself if it is open. If start belongs to closed_stations, the dispatcher considers the coverage to be zero. Roads that lead to closed stations should be ignored, even if alternate routes exist. Provide the final answer as an integer.

Example 1:

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.

Example 2:

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 3:

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.

00:00
Loading editor...
Test Results

Run your code to see test results

Click the Submit button to execute your solution

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.