Imagine you're navigating through a unique road network where each intersection has at most one outgoing road. This creates an interesting graph structure that might contain cycles or dead ends!
You're given a directed graph of n nodes numbered from 0 to n-1. The graph is represented by an array edges where edges[i] tells you which node you can reach from node i. If there's no outgoing edge from node i, then edges[i] = -1.
Your mission: Find a meeting point that both node1 and node2 can reach, such that the maximum distance either traveler needs to walk is minimized. If multiple meeting points exist with the same optimal distance, choose the one with the smallest index.
Key Challenge: The graph may contain cycles, so you need to handle infinite loops carefully!
Input & Output
Constraints
- n == edges.length
- 2 โค n โค 105
- edges[i] == -1 or 0 โค edges[i] < n
- edges[i] != i (no self-loops in input)
- 0 โค node1, node2 < n