<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:
    vector<int> shortestAlternatingPaths(int n, vector<vector<int>>& redEdges, vector<vector<int>>& blueEdges) {
        unordered_map<string, vector<string>> graph;
        graph.insert({ "0r", vector<string>() });
        graph.insert({ "0b", vector<string>() });

        for (auto &e: redEdges) {
            string src = to_string(e[0]) + "r";
            string dst = to_string(e[1]) + "b";

            if (!graph.count(src)) graph.insert({ src, vector<string>() });
            graph[src].push_back(dst);
        }

        for (auto &e: blueEdges) {
            string src = to_string(e[0]) + "b";
            string dst = to_string(e[1]) + "r";

            if (!graph.count(src)) graph.insert({ src, vector<string>() });
            graph[src].push_back(dst);
        }

        unordered_map<string, bool> vis;
        unordered_map<string, int> dist;

        queue<string> q;
        dist["0r"] = 0;
        dist["0b"] = 0;
        vis["0r"] = true;
        vis["0b"] = true;
        q.push("0r");
        q.push("0b");

        for (int i = 0; i < n; i++) {
            vis.insert({ to_string(i) + "r", false });
            vis.insert({ to_string(i) + "b", false });
            dist.insert({ to_string(i) + "r", -1 });
            dist.insert({ to_string(i) + "b", -1 });
        }

        while (!q.empty()) {
            string u = q.front(); q.pop();
            for (auto v: graph[u]) {
                if (!vis[v]) {
                    vis[v] = true;
                    dist[v] = dist[u] + 1;
                    q.push(v);
                }
            }
        }

        vector<int> ret(n, -1);
        for (int i = 0; i < n; i++) {
            string key1 = to_string(i) + "r";
            string key2 = to_string(i) + "b";

            int ans1 = dist[key1];
            int ans2 = dist[key2];

            if (ans1 == -1 && ans2 == -1) {
                ret[i] = -1;
            } else if (ans1 == -1) {
                ret[i] = ans2;
            } else if (ans2 == -1) {
                ret[i] = ans1;
            } else {
                ret[i] = min(ans1, ans2);
            }
        }

        return ret;
    }
};
24260