<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>

<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicating that there is a directed edge from node <code>i</code> to node <code>edges[i]</code>. If there is no outgoing edge from <code>i</code>, then <code>edges[i] == -1</code>.</p>

<p>You are also given two integers <code>node1</code> and <code>node2</code>.</p>

<p>Return <em>the <strong>index</strong> of the node that can be reached from both </em><code>node1</code><em> and </em><code>node2</code><em>, such that the <strong>maximum</strong> between the distance from </em><code>node1</code><em> to that node, and from </em><code>node2</code><em> to that node is <strong>minimized</strong></em>. If there are multiple answers, return the node with the <strong>smallest</strong> index, and if no possible answer exists, return <code>-1</code>.</p>

<p>Note that <code>edges</code> may contain cycles.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-2.png" style="width: 321px; height: 161px;" />
<pre>
<strong>Input:</strong> edges = [2,2,3,-1], node1 = 0, node2 = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> The distance from node 0 to node 2 is 1, and the distance from node 1 to node 2 is 1.
The maximum of those two distances is 1. It can be proven that we cannot get a node with a smaller maximum distance than 1, so we return node 2.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-4.png" style="width: 195px; height: 161px;" />
<pre>
<strong>Input:</strong> edges = [1,2,-1], node1 = 0, node2 = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> The distance from node 0 to node 2 is 2, and the distance from node 2 to itself is 0.
The maximum of those two distances is 2. It can be proven that we cannot get a node with a smaller maximum distance than 2, so we return node 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == edges.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>-1 &lt;= edges[i] &lt; n</code></li>
	<li><code>edges[i] != i</code></li>
	<li><code>0 &lt;= node1, node2 &lt; n</code></li>
</ul>

class Solution:
    def closestMeetingNode(self, edges: List[int], node1: int, node2: int) -> int:
        # since edges are all equal value, min distance can be found with 
        # bfs. dfs is also especially easy since nodes only point to 1 other
        # node (or none). Exhaustive way: Using two traversals of the graph, 
        # one from node1 and another from node2, find the min distance
        # from node1 to every node and node2 to every node. Then go through
        # list and find either that a) the node with the smallest
        # max(dist_from_1, dist_from_2) value or b) no node could be reached
        # by both, return -1.

        # NOTE: since each node has max 1 outgoing edge, bfs and dfs are
        # identical in this problem. each level in bfs points to 1 node,
        # and each step in dfs only has 1 option to go to next. Hence, the
        # choice of what nodes to visit next results in the same thing. Kind
        # of neat imo

        # Somewhat faster way (don't think time save is guaranteed especially
        # when there is no common node found, but practically could help):
        # start each traversal at node1 and node2, move 1 step for both
        # traversals at a time, return immediately if a common node is found
        # and cost to get there will be what iteration we are on. Since
        # any common node in the future takes at least one more step, the
        # min distance will be greater for either node1 or node2. Saves
        # future iterations that node1 and node2 had.

        # need 2 visited sets since the nodes seen by node1 should
        # not be visited by node1's traversal but should be visited by
        # node2's traveral (or else how are we gonna find its dist?).

        # NOTE: one way traversal could end with no common node is no
        # a different dead end for each traversal with no common node, but
        # not necessary the case. Maybe traversal1 is in a cycle and 
        # traversal2 is in a separate cycle, they will never run out of
        # nodes but will never find a common node. So there are 2 ways to
        # quit: if both reach a dead end (no next node) or if both find
        # cycles

        current1 = node1
        seen1 = set()
        in_cycle1 = False

        current2 = node2
        seen2 = set()
        in_cycle2 = False

        iterations = 0
        # left checks if at least one traversal not in cycle, right side
        # checks if at least one node not reached a dead end
        while (in_cycle1 == False or in_cycle2 == False) and (current1 != -1 or current2 != -1):

            
            
            if current1 in seen1:
                in_cycle1 = True
            if current2 in seen2:
                in_cycle2 = True
            
            # NOTE: this makes it so a dead end (-1) is put in the seen list,
            # so if the other traversal makes it to a dead end it will return
            # -1 from inside this for loop, that behavior is fine
            seen1.add(current1)
            seen2.add(current2)

            # if found current node in the opposite traversal, we have found
            # the first common node
            # maybe both current1 and current2 refer to "correct" answers. If
            # so return the min node
            if current1 in seen2 and current2 in seen1:
                return min(current1, current2)
            elif current1 in seen2:
                return current1
            elif current2 in seen1:
                return current2

            # did not find a cycle or a common node, keep traversing if
            # not at a dead end (1 could be at a dead end, not both)
            if current1 != -1:
                current1 = edges[current1]
            if current2 != -1:
                current2 = edges[current2]

            iterations += 1

        return -1
416