<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 {
    pair<pair<int, int>, int> dist; // Stores node and distance traversed, and previously used colour
public:


    vector<int> answer;
    bool visited[101][2];
    void bfs(map<int, vector<int>> blueGraph, map<int, vector<int>> redGraph, int colour) {
        queue<pair<pair<int, int>, int>> q;
        q.push(make_pair(make_pair(0, 0), colour));
        visited[0][colour] = true;
        answer[0] = 0;
        while(!q.empty()) {
            pair<pair<int, int>, int> element = q.front();
            q.pop();
            int node = element.first.first;
            int distance = element.first.second;
            int colour = element.second;
            
            cout<<"Traversing "<<node<<", with dist = "<<distance<<", colour = "<<colour<<endl;
            if (colour == 0) {
                for (int i = 0; i < redGraph[node].size(); i++) {
                    int neighbour = redGraph[node][i];
                    cout<<"\tTraversing neighbour"<<neighbour<<endl;
                    if (!visited[neighbour][colour]) {
                        answer[neighbour] = min(answer[neighbour], distance+1);
                        q.push(make_pair(make_pair(neighbour, distance + 1), 1 - colour));
                    }
                    visited[neighbour][colour] = true;


                }
            } else {
                for (int i = 0; i < blueGraph[node].size(); i++) {
                    int neighbour = blueGraph[node][i];
                    if (!visited[neighbour][colour]) {
                        answer[neighbour] = min(answer[neighbour], distance+1);
                        q.push(make_pair(make_pair(neighbour, distance + 1), 1 - colour));
                    }
                    visited[neighbour][colour] = true;

                }
            }

        }
    } 


    vector<int> shortestAlternatingPaths(int n, vector<vector<int>>& redEdges, vector<vector<int>>& blueEdges) {
        
        int size = n-1;

        // Construct adj. lists for both colours
        map<int, vector<int>> redGraph;
        for(auto edge: redEdges) {
            if (redGraph.find(edge[0]) != redGraph.end()) {
                redGraph[edge[0]].push_back(edge[1]);
                
            } else {
                vector<int> temp;
                temp.push_back(edge[1]);
                redGraph[edge[0]] = temp;
            }
        }

        map<int, vector<int>> blueGraph;
        for(auto edge: blueEdges) {
            if (blueGraph.find(edge[0]) != blueGraph.end()) {
                blueGraph[edge[0]].push_back(edge[1]);
            } else {
                vector<int> temp;
                temp.push_back(edge[1]);
                blueGraph[edge[0]] = temp;
            }
        }
        for(int i = 0 ; i <= size; i++) {
            answer.push_back(INT_MAX);
        }
        // BFS over the graph with alternating colours
        for(int i = 0; i < 101; i++) {
            visited[i][0] = false;
            visited[i][1] = false;
        }
        bfs(blueGraph, redGraph, 0);

        for(int i = 0; i < 101; i++) {
            visited[i][0] = false;
            visited[i][1] = false;
        }
        bfs(blueGraph, redGraph, 1);

        // Manipulate INT_MAX to -1
        for(int i = 0; i <= size; i++) {
            if (answer[i] == INT_MAX) {
                answer[i] = -1;
            }
        }
        return answer;
    }
};
23884