Wildlife Bridge Network Scan
The wildlife corridor committee oversees a string of migration bridges that allow animals to cross a busy valley. Each bridge is labeled from 0 to n - 1, and conservationists catalog every footpath that connects two bridges in either direction. During certain seasons, specific crossings must close for habitat restoration. When rangers begin a survey at one bridge, they hike across open footpaths, notifying each accessible crossing, while avoiding any bridge that is temporarily closed.
Your task is to implement a function that accepts the number of bridges n, a list of footpaths paths, a starting bridge start, and a set closed_bridges identifying crossings that are off-limits. Each path is represented by [u, v], meaning hikers may walk between bridges u and v without restriction when both endpoints are open. The list can contain duplicates, reversed pairs, or self-loops that describe inspection turnarounds. The starting bridge is always open. Rangers skip any path that would step onto a closed bridge, and they do not count that location in the final survey.
Return the total number of open bridges reached during the survey, including the starting bridge. Closed bridges are never counted even if alternate routes exist. If every neighboring crossing is closed, the survey covers only the starting location. When all crossings are open, the result matches the size of the connected component containing start. Make sure repeated entries do not inflate the total and that the order of the list does not affect the answer.
Example 1:
Input: n = 6, paths = [[0,1],[1,2],[2,3],[3,4],[2,5]], start = 0, closed_bridges = [4]
Output: 5
Explanation: The survey reaches bridges 0, 1, 2, 3, and 5; bridge 4 remains closed.
Example 2:
Input: n = 5, paths = [[0,1],[1,2],[2,3],[3,4]], start = 2, closed_bridges = [1,3]
Output: 1
Explanation: Bridges 1 and 3 are closed, so hikers remain at bridge 2 only.
Example 3:
Input: n = 7, paths = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6]], start = 4, closed_bridges = []
Output: 4
Explanation: All bridges are open, so the survey covers bridges 4, 3, 5, and 6.
Related Problems
No related problems found
Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
