You are given an integer n, the number of nodes in a directed graph where the nodes are labeled from 0 to node n - 1. Each edge is red or blue in this graph, and there could be self-edges and parallel edges.
You are given two arrays redEdges and blueEdges where:
redEdges[i] = [ai, bi] indicates that there is a directed red edge from node ai to node bi in the graph, and
blueEdges[j] = [uj, vj] indicates that there is a directed blue edge from node uj to node vj in the graph.
Return an array answer of length n, where each answer[x] is the length of the shortest path from node 0 to node x such that the edge colors alternate along the path, or -1 if such a path does not exist.
The shortest path with alternating colors can be found using BFS, but we must track the color of the last edge used to ensure alternation. By exploring both red and blue edges from each node, and keeping track of the color used to reach each node, we avoid revisiting the same state and ensure the shortest path is found.
classSolution {
publicint[]shortestAlternatingPaths(int n, int[][] redEdges, int[][] blueEdges) {
List<Integer>[] red =new List[n], blue =new List[n];
for (int i = 0; i < n; ++i) red[i]=new ArrayList<>();
for (int i = 0; i < n; ++i) blue[i]=new ArrayList<>();
for (int[] e : redEdges) red[e[0]].add(e[1]);
for (int[] e : blueEdges) blue[e[0]].add(e[1]);
int[][] dist =newint[n][2];
for (int[] d : dist) Arrays.fill(d, Integer.MAX_VALUE);
dist[0][0]= dist[0][1]= 0;
Queue<int[]> q =new LinkedList<>();
q.offer(newint[]{0,0});
q.offer(newint[]{0,1});
while (!q.isEmpty()) {
int[] cur = q.poll();
int u = cur[0], c = cur[1];
List<Integer>[] next = c == 0 ? blue : red;
for (int v : next[u]) {
if (dist[v][1-c]== Integer.MAX_VALUE) {
dist[v][1-c]= dist[u][c]+ 1;
q.offer(newint[]{v,1-c});
}
}
}
int[] ans =newint[n];
for (int i = 0; i < n; ++i) {
int d = Math.min(dist[i][0], dist[i][1]);
ans[i]= d == Integer.MAX_VALUE?-1 : d;
}
return ans;
}
}
classSolution {
funshortestAlternatingPaths(n: Int, redEdges: Array<IntArray>, blueEdges: Array<IntArray>): IntArray {
val red = Array(n) { mutableListOf<Int>() }
val blue = Array(n) { mutableListOf<Int>() }
for (e in redEdges) red[e[0]].add(e[1])
for (e in blueEdges) blue[e[0]].add(e[1])
val dist = Array(n) { IntArray(2) { Int.MAX_VALUE } }
dist[0][0] = 0; dist[0][1] = 0val q = ArrayDeque<Pair<Int,Int>>()
q.add(0 to 0); q.add(0 to 1)
while (q.isNotEmpty()) {
val(u, c) = q.removeFirst()
val next = if (c ==0) blue else red
for (v in next[u]) {
if (dist[v][1-c] ==Int.MAX_VALUE) {
dist[v][1-c] = dist[u][c] + 1 q.add(v to 1-c)
}
}
}
return IntArray(n) {
val d = minOf(dist[it][0], dist[it][1])
if (d ==Int.MAX_VALUE) -1else d
}
}
}
classSolution:
defshortestAlternatingPaths(self, n: int, redEdges: list[list[int]], blueEdges: list[list[int]]) -> list[int]:
red = [[] for _ in range(n)]
blue = [[] for _ in range(n)]
for u, v in redEdges: red[u].append(v)
for u, v in blueEdges: blue[u].append(v)
dist = [[float('inf')] *2for _ in range(n)]
dist[0][0] = dist[0][1] =0from collections import deque
q = deque([(0,0),(0,1)])
while q:
u, c = q.popleft()
next_edges = blue if c ==0else red
for v in next_edges[u]:
if dist[v][1-c] == float('inf'):
dist[v][1-c] = dist[u][c] +1 q.append((v,1-c))
ans = []
for d0, d1 in dist:
d = min(d0, d1)
ans.append(-1if d == float('inf') else d)
return ans