Represent a state by (phase, Tom vertex, Jerry vertex), where phase 0 means Jerry moves next and phase 1 means Tom moves next. Add staying to each vertex's legal move list. Equal-position states are terminal at distance zero in both phases. Distances count individual player moves, including a move that causes capture.
Work backward from all terminal states with a FIFO queue. At a Tom state, one winning successor suffices: its minimax distance is one plus the minimum successor distance. At a Jerry state, every legal successor must be winning: its minimax distance is one plus the maximum successor distance. Maintain a remaining-successor counter for each Jerry state. Because the graph is undirected, predecessor positions are obtained from the same neighbor-plus-stay lists. A resolved phase-0 state informs Tom predecessors; a resolved phase-1 state informs Jerry predecessors.
All terminal distances are zero. Queue processing is nondecreasing in distance: every newly resolved state gets the currently popped distance plus one. Therefore the first resolved successor of a Tom predecessor is a minimum, and the last required resolved successor of a Jerry predecessor is a maximum. This establishes the exact min/max recurrence inductively and resolves each state only once. For a Jerry state, the counter reaches zero only after all distinct legal choices have resolved. Unique edges and one explicit stay entry ensure each choice is counted once.
When the queue empties, an unresolved Tom state has no resolved successor, while an unresolved Jerry state retains at least one unresolved successor. Jerry can always choose such a successor and Tom cannot leave the unresolved set, so capture can be avoided forever. Conversely, a resolved state has a strategy forcing the terminal set with the computed finite bound. Thus unresolved states return -1, including starts in different components.
The initial state has Jerry to move. If the minimax capture distance is h individual moves, the capture second is ceil(h/2), implemented as (h+1)//2. This also returns zero for initial capture. Counting a possible immediate capture after either phase gives the same second for the two moves of that second. The ceiling function is monotone and commutes with finite minimum and maximum, so converting the optimal individual-move bound preserves the optimal guaranteed number of seconds.
For n vertices and m edges, there are 2nn states and O(n*(n+m)) transitions including stays. Every state and transition is processed at most once. Time is O(n*(n+m)); distance, counters, and queue use O(nn), and adjacency uses O(n+m), giving O(nn+m) space.
Two BFS arrays from the initial positions describe only static shortest paths. They omit whose turn is next, Tom's future responses, and Jerry's choices after those responses; a finite cycle can support evasion forever despite all relevant BFS distances being finite. A shortcut would need additional assumptions and a separate proof. Restricting the graph to a tree removes cycles and may support a tree-specific pursuit argument, but does not by itself justify an arbitrary formula from two distances. Alternatively, changing the objective to comparing independent shortest arrival times to a fixed vertex makes the initial BFS distances directly relevant. Neither change is part of this game, so the implementation uses the full state-space analysis.