<p>You are given an integer <code>n</code>, the number of nodes in a directed graph where the nodes are labeled from <code>0</code> to <code>n - 1</code>. Each edge is red or blue in this graph, and there could be self-edges and parallel edges.</p>

<p>You are given two arrays <code>redEdges</code> and <code>blueEdges</code> where:</p>

<ul>
	<li><code>redEdges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is a directed red edge from node <code>a<sub>i</sub></code> to node <code>b<sub>i</sub></code> in the graph, and</li>
	<li><code>blueEdges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates that there is a directed blue edge from node <code>u<sub>j</sub></code> to node <code>v<sub>j</sub></code> in the graph.</li>
</ul>

<p>Return an array <code>answer</code> of length <code>n</code>, where each <code>answer[x]</code> is the length of the shortest path from node <code>0</code> to node <code>x</code> such that the edge colors alternate along the path, or <code>-1</code> if such a path does not exist.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
<strong>Output:</strong> [0,1,-1]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3, redEdges = [[0,1]], blueEdges = [[2,1]]
<strong>Output:</strong> [0,1,-1]
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>0 &lt;= redEdges.length,&nbsp;blueEdges.length &lt;= 400</code></li>
	<li><code>redEdges[i].length == blueEdges[j].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> &lt; n</code></li>
</ul>

class Solution {
public:

    class Node{
        public:
            int cp;
            int cc;
            int cl;
            int n;
            Node(int cp_, int cc_, int cl_, int n_){
                cp = cp_;
                cc = cc_;
                cl = cl_;
                n = n_;
            }
    };

    int getDist(int s, int &e, vector<vector<bool>> &adjMR, vector<vector<bool>> &adjMB){
        queue<Node*> q;
        int n = adjMR.size();
        vector<pair<bool,bool>> cv(n,{false,false});
        cv[0] = {true,true};
        q.push(new Node(0,0,0,n));
        int ans = INT_MAX;
        while(!q.empty()){
            auto cur = q.front();
            if(cur->cp == e){
                ans = min(ans,cur->cl);
                break;
            }
            q.pop();
            for(int i = 0 ; i < n ; i++){
                if(cur->cc == 0){
                     if(cv[i].first)
                        continue;
                    if(adjMR[cur->cp][i]){
                        cv[i].first = true;
                        q.push(new Node(i,1,cur->cl+1,n));
                        //cur->cv[i].first = false;
                    }            
                }else{
                    if(cv[i].second)
                        continue;
                    if(adjMB[cur->cp][i]){
                        cv[i].second = true;
                        q.push(new Node(i,0,cur->cl+1,n));
                        //cur->cv[i].second = false;
                    }
                }
            }
        }
        q = queue<Node*>();
        cv = vector<pair<bool,bool>>(n,{false,false});
        cv[0] = {true,true};
        q.push(new Node(0,1,0,n));
        while(!q.empty()){
            auto cur = q.front();
            if(cur->cp == e){
                ans = min(ans,cur->cl);
                break;
            }
            q.pop();
            for(int i = 0 ; i < n ; i++){
                if(cur->cc == 0){
                    if(cv[i].first)
                        continue;
                    if(adjMR[cur->cp][i]){
                        cv[i].first = true;
                        q.push(new Node(i,1,cur->cl+1,n));
                        //cur->cv[i].first = false;
                    }            
                }else{
                    if(cv[i].second)
                        continue;
                    if(adjMB[cur->cp][i]){
                        cv[i].second = true;
                        q.push(new Node(i,0,cur->cl+1,n));
                        //cur->cv[i].second = false;
                    }
                }
            }
        }
        if(ans == INT_MAX)
            return -1;
        return ans;
    }


    vector<int> shortestAlternatingPaths(int n, vector<vector<int>>& redEdges, vector<vector<int>>& blueEdges) {
        vector<vector<bool>> adjMR(n,vector<bool>(n,false));
        vector<vector<bool>> adjMB(n,vector<bool>(n,false));
        for(auto edge : redEdges){
            adjMR[edge[0]][edge[1]] = true;
        }
        for(auto edge : blueEdges){
            adjMB[edge[0]][edge[1]] = true;
        }
        vector<int> ans(n,INT_MAX);
        ans[0] = 0;
        for(int i = 1 ; i < n ; i++){
            ans[i] = getDist(0,i,adjMR,adjMB);
        }
        return ans;
    }
};
29151