We’re preparing your current view and syncing the latest data.
You are given a graph with n nodes labeled from 0 to n - 1. The edges are colored either red or blue. Find the length of the shortest path from node 0 to every other node such that edges alternate in color. If there is no such path to a node, return -1 for that node.
An integer n representing the number of nodes, a list redEdges where each element is [from, to] representing red edges, and a list blueEdges for blue edges.
An integer array answer of length n where answer[i] is the length of the shortest alternating color path from node 0 to node i or -1 if no such path exists.
1 <= n <= 100, 0 <= redEdges.length, blueEdges.length <= 400, redEdges[i].length == blueEdges[i].length == 2, 0 <= redEdges[i][j], blueEdges[i][j] < n
Example 1
Input
n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
Output
[0,1,-1]
Explanation
Node 0 to 1 can be reached by a red edge (distance 1), but node 2 cannot be reached via alternating colors since no blue edges exist.