Ward Reverse-Pass Rounds
Maximum distinct vertex reward on a closed walk with one reversal
A hospital ward has rooms connected by one-way corridors. Room i has an inspection value of rewards[i], and corridors[i] lists the rooms that can be entered directly from room i by following a corridor in its normal direction.
An inspector starts in room start and must finish in that same room. During the entire round, the inspector may traverse at most one corridor once against its direction. Every other traversal must follow a corridor's normal direction. Using this permission does not change the corridor's direction for later traversals.
Rooms and corridors may be visited repeatedly, but each room's inspection value is collected only on its first visit. The starting room's value is collected immediately. Taking no steps is allowed.
Return the maximum total inspection value the inspector can collect on such a round.
Repeated entries in a corridor list represent parallel corridors, and a corridor may lead back to its own room.
Examples
Example 1
Input: rewards = [4,7,2,9,3], corridors = [[1],[2],[],[2,4],[0]], start = 0 Output: 25
Follow rooms 0, 1, and 2, traverse the corridor from 3 to 2 against its direction to enter room 3, and then follow rooms 4 and 0. This legal round visits every room.
Example 2
Input: rewards = [5,1,8,4], corridors = [[1],[0,2],[3],[]], start = 0 Output: 14
Rooms 0 and 1 can be explored freely in both directions. The inspector can then visit room 2 and reverse the corridor from 1 to 2 to return. Visiting room 3 would leave the inspector unable to return with only one reverse traversal.
Example 3
Input: rewards = [6,10,3], corridors = [[],[2],[1]], start = 0 Output: 6
The starting room has no incoming or outgoing corridors, so the inspector must remain there.
Constraints
- 1 <= rewards.length <= 100000
- corridors.length == rewards.length
- 0 <= rewards[i] <= 1000000
- 0 <= start < rewards.length
- Every entry in corridors[i] is a valid room index.
- The total number of entries across all corridor lists is at most 200000.
- Self-loops and parallel corridors are allowed.
Use iterative graph traversals to avoid recursion-depth limits. The intended solution takes O(n + m) time and space. After contracting strongly connected components, the two paths joined by the reversed edge cannot share a component other than the starting component: any other shared component would form a directed cycle with the starting component. Thus their collected values can be added, subtracting the starting component's value once. All possible answers fit exactly in a JavaScript Number.
Hints
Show hint 1Hint 1
Within a strongly connected component, a walk can collect every room's value and still reach any desired exit. Contract these components.
Show hint 2Hint 2
In the resulting directed acyclic graph, consider an edge a -> b used backward. Combine a best path from the starting component to b with a best path from a back to the starting component. Why can these paths share no other component?
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you reconstruct an optimal round as a sequence of corridor traversals?
- Why does the same component-weight approach fail if room values can be negative and visiting a room automatically collects its value?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem