<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 {
public:
    int closestMeetingNode(vector<int>& edges, int node1, int node2) {
    //     int n=edges.size();
    //     vector<int> d1(n,-1);
    //     vector<int> d2(n,-1);
    //     queue<pair<int,int>> q;
    //     q.push({node1,0});
    //     while(!q.empty()){
    //         int node=q.front().first;
    //         int dis=q.front().second;
    //         q.pop();
    //         if(d1[node]==-1){
    //         d1[node]=dis;
    //         if(edges[node]!=-1)q.push({edges[node],dis+1});
    //         }
    //     }
    //     q.push({node2,0});
    //     while(!q.empty()){
    //         int node=q.front().first;
    //         int dis=q.front().second;
    //         q.pop();
    //         if(d2[node]==-1){
    //         d2[node]=dis;
    //         if(edges[node]!=-1)q.push({edges[node],dis+1});
    //         }
    //     }
    //     if(d1[node2]!=-1&&d2[node1]==-1)return node2;
    //     else if(d2[node1]!=-1&&d1[node2]==-1)return node1;
    //     else if(d2[node1]!=-1&&d1[node2]!=-1){
    //         if(d2[node1]>d1[node2]){
    //             return node2;
    //         }
    //         else if(d2[node1]<d1[node2]){
    //             return node1;
    //         }
    //         else return min(node1,node2);
    //     }
    //     else{
    //         int ans=-1;
    //         int dis=INT_MAX;
    //     for(int i=0;i<n;i++){
    //       if(d1[i]!=-1&&d2[i]!=-1)
    //         if(dis>max(d1[i],d2[i])){
    //             dis=max(d1[i],d2[i]);
    //             ans=i;
    //         }
    //         if(dis==max(d1[i],d2[i])&&i<ans){
    //             ans=i;
    //         };
    //     }
    //     return ans;
    // }
    int n = edges.size();
    vector<int> d1(n, -1);
    vector<int> d2(n, -1);

    // BFS from node1
    queue<pair<int, int>> q;
    q.push({node1, 0});
    while (!q.empty()) {
        int node = q.front().first;
        int dis = q.front().second;
        q.pop(); // Pop the current element from the queue
        if (d1[node] == -1) { // Process the node only if it hasn't been visited
            d1[node] = dis;
            if (edges[node] != -1) {
                q.push({edges[node], dis + 1});
            }
        }
    }

    // BFS from node2
    q.push({node2, 0});
    while (!q.empty()) {
        int node = q.front().first;
        int dis = q.front().second;
        q.pop(); // Pop the current element from the queue
        if (d2[node] == -1) { // Process the node only if it hasn't been visited
            d2[node] = dis;
            if (edges[node] != -1) {
                q.push({edges[node], dis + 1});
            }
        }
    }

    // Find the closest meeting node
    int ans = -1;
    int minDist = INT_MAX;
    for (int i = 0; i < n; i++) {
        if (d1[i] != -1 && d2[i] != -1) {
            int maxDist = max(d1[i], d2[i]);
            if (maxDist < minDist) {
                minDist = maxDist;
                ans = i;
            } else if (maxDist == minDist && i < ans) {
                ans = i;
            }
        }
    }

    return ans;
     }
};
105384