n nodes numbered from 0 to n - 1 and one-way roads of two colors: red[i] = [a, b] is a red road from a to b, and blue[j] = [a, b] is a blue one. Loops (a road from a node to itself) and several roads between the same pair of nodes are possible.
A route from node 0 is allowed if the colors along it alternate: a red road is followed by a blue one and a blue road by a red one. The first road may be either color.
Return a list of length n whose entry x is the fewest roads on an allowed route from 0 to x, or -1 if there is no such route. For node 0 itself the answer is 0.n=4, red=[[0, 1], [2, 3]], blue=[[1, 2], [0, 2]] → [0, 1, 1, 2]
Node 1 is one red road away, node 2 one blue road away. Node 3: blue 0 → 2 then red 2 → 3, 2 roads. The route 0 → 1 → 2 → 3 also alternates but is longer.
n=3, red=[[0, 1], [1, 2]], blue=[] → [0, 1, -1]
Node 2 can only be reached by two red roads in a row, which is not allowed, so its answer is -1.
n=4, red=[[0, 1], [2, 3]], blue=[[1, 2], [0, 2]]
[0, 1, 1, 2]
Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.