Campus Shuttle Loop Coverage
The campus operations team manages a shuttle loop that ferries students between academic buildings. Each stop is identified by a number from 0 to n - 1, and the transportation office records every walkway that allows the shuttle to travel in both directions. When the drivers start at a particular stop, they move along connected walkways and announce the shuttle arrival in each distinct building. To keep the schedule tight, they skip any walkway that would lead them back into a building already served during the loop. Facility planners want to know how many buildings can be covered under this rule set.
Implement a function that accepts the total number of buildings n, a list of walkways, and the starting stop start. Each walkway is represented as [u, v], indicating a two-way path between building u and building v. The list might contain duplicate entries if multiple departments submitted the same path, and some entries may even point from a building to itself due to temporary staging areas. The shuttle loop travels from building to building, ignoring any future path that would re-enter an already visited building.
Return the total count of unique buildings served during a sweep, including the starting stop. Buildings that cannot be reached from start never receive the announcement. If start has no connecting walkway, the loop covers only that building. In a fully connected campus, the shuttle will report in every building exactly once. The function must avoid double-counting even when there are multiple distinct paths between the same pair of buildings.
Example 1:
Input: n = 6, walkways = [[0,1],[1,2],[2,3],[3,4],[4,5]], start = 0
Output: 6
Explanation: The shuttle stops in every building along the loop without revisiting any stops.
Example 2:
Input: n = 5, walkways = [[0,1],[1,2],[2,0],[3,4]], start = 2
Output: 3
Explanation: Buildings 0, 1, and 2 are visited; the remaining pair sits in another component.
Example 3:
Input: n = 4, walkways = [[0,1],[2,3]], start = 1
Output: 2
Explanation: The loop covers buildings 1 and 0, while the other set stays untouched.
Related Problems
No related problems found
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
