The array edges describes a directed graph with n = edges.length nodes labeled 0 through n - 1. A value edges[i] = j gives an edge from i to j, while -1 means node i has no outgoing edge. Cycles are allowed.
Find a node reachable from both node1 and node2 that minimizes the larger of the two shortest-path distances to it. A distance is the number of edges traveled; a node is distance zero from itself.
Return the chosen node's label. If several nodes achieve the same minimum, return the smallest label. If no node is reachable from both starting nodes, return -1.
Example 1:
Input: edges = [1,2,3,0]
node1 = 0
node2 = 2
Output: 0Explanation: Nodes 0 and 2 both have maximum distance 2 from the two starting nodes. Nodes 1 and 3 have maximum distance 3. The tie between 0 and 2 is resolved by choosing the smaller label, 0.
Example 2:
Input: edges = [1,-1,3,-1]
node1 = 0
node2 = 2
Output: -1Explanation: Node 0 can reach only nodes 0 and 1, while node 2 can reach only nodes 2 and 3. There is no common reachable node.
Example 3:
Input: edges = [2,2,3,-1]
node1 = 1
node2 = 1
Output: 1Explanation: Both starting nodes are node 1. Its distance from itself is 0, which is the smallest possible maximum distance.
Constraints:
n == edges.length2 <= n <= 100000-1 <= edges[i] < nedges[i] != i0 <= node1, node2 < n