<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:
    def shortestAlternatingPaths(self, n: int, redEdges: List[List[int]], blueEdges: List[List[int]]) -> List[int]:
        '''
        i think this is a dijkstra's algorithm or backtracking problem

        we want the shortest path from node 0 to node x, where x is each node in graph of size n

        naive way:
        instantiate res array of size n, all values inf. set res[0] to 0 because 0 -> 0 is 0

        build directed graphs:
        2 graphs, red and blue
        ex: red[0] = [1, 2]

        find the subarrays where node 0 is start
        start with node 0 with a red edge. alternate. track number of movements 
        start with node 0 with a blue edge. alternate

        if a path has not been seen before, change it
        if it has, set path equal to smaller value

        bfs
        seen - using this set will make sure we reach each node at their shortest paths
            track node and color
        queue - node, numMoves, color
        initially i should add 0 with both colors
        if 0 does not have an edge for a specific color, nothing will happen
        deque
        if node has a nei, add nei, inc numMoves, and change color

        at the end, if a value is inf in res, set to -1
        '''
        red = collections.defaultdict(list)
        blue = collections.defaultdict(list)

        for [a,b] in redEdges:
            red[a].append(b)
        
        for [u, v] in blueEdges:
            blue[u].append(v)
        
        res = [-1] * n
        res[0] = 0

        queue = deque()
        seen = set()
        queue.append((0, 0, 'red'))
        queue.append((0, 0, 'blue'))
        seen.add((0, 'red'))
        seen.add((0, 'blue'))
        
        while queue:
            node, numMoves, color = queue.popleft()
            if res[node] == -1:
                res[node] = numMoves
            if color == 'blue': # want to traverse the opposite color
                for nei in red[node]:
                    if (nei, 'red') not in seen:
                        seen.add((nei, 'red'))
                        queue.append((nei, numMoves + 1, 'red'))
            if color == 'red':
                for nei in blue[node]:
                    if (nei, 'blue') not in seen:
                        seen.add((nei, 'blue'))
                        queue.append((nei, numMoves + 1, 'blue'))
        return res
16900