<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:
        adj = defaultdict(list)
        for i, val in enumerate(edges):
            adj[i].append(val)

        node1_dis = self.bfs(adj, node1)
        node2_dis = self.bfs(adj, node2)

        node_max = {}
        for i in range(len(edges)):
            dis1 = inf
            dis2 = inf
            if i in node1_dis:
                dis1 = node1_dis[i]
            if i in node2_dis:
                dis2 = node2_dis[i]
            node_max[i] = max(dis1, dis2)

        # print(node_max)
        final_node = -1
        min_ans = inf
        for i in node_max:
            if node_max[i] < min_ans:
                min_ans = node_max[i]
                final_node = i
            if node_max[i] == min_ans and i < final_node:
                final_node = i
        if min_ans == inf:
            return -1
        return final_node

        
    def bfs(self, graph, node):
        queue = collections.deque()
        queue.append(node)
        node_dis = {}
        level = 0
        visited = set()

        while queue:
            l = len(queue)
            for i in range(l):
                popped_node = queue.popleft()

                if popped_node not in visited:
                    visited.add(popped_node)
                    node_dis[popped_node] = level

                    for node in graph[popped_node]:
                        if node != -1 and node not in visited:
                            queue.append(node)
            level += 1
        return node_dis
        



















































































    #     dic = {}
    #     for i in range(len(edges)):
    #         if i in dic:
    #             l = dic[i]
    #             l.append(edges[i])
    #             dic[i] = l
    #         else:
    #             dic[i] = [edges[i]]

    #     queue.append(node1)
    #     queue.append(node2)

        
    #     first_bfs = set()
    #     second_bfs = set()
    #     queue = collections.deque()
    #     queue.append((node1, 0))
    #     self.bfs(queue, dic, first_bfs)
    #     queue = collections.deque()
    #     queue.append((node2, 0))
    #     self.bfs(queue, dic, second_bfs)

    # #     print(first_bfs, second_bfs)

    # def bfs(self, queue, dic, bfs_dic):
    #     visited = set()
    #     while queue:
    #         node = queue.popleft()
    #         if node in visited:
    #             visited.add(node)

    #             if node in dic:
    #                 for i in dic[node]:
    #                     if i not in visited:
    #                         queue.append((i, node))
        

    # #     return






        
69786