New update is available. Click here to update.

Last Updated: 15 Jan, 2021

Difficulty: Moderate

```
1. This Graph may contain self-loops and parallel edges.
2. The Graph will be directed which means it’s edges are one-directional (from some node u to some node v).
3. Alternating distance is the number of edges between two nodes such that the colours of each consecutive edge in the path are different.
4. If there is no alternating path from a node X to the node with label-0 then print -1 as an answer corresponding to that node.
```

```
The first line of the input contains an integer 'T', denoting the number of test cases.
The first line of each test case contains three space-separated integers 'N', 'R' and 'G', denoting the number of nodes, number of Red edges and number of Green edges in the graph respectively.
The next 'R' lines of each test case contain two space-separated integers u and v, denoting Red coloured directed edges from a node with label u to a node with label v.
The next 'G' lines of each test case contain two space-separated integers u and v, denoting Green coloured directed edges from a node with label u to the node with label v.
```

```
For each test case, print a single line containing 'N' space-separated integers denoting the shortest alternating path length of each node from the node with label 0.
The output of each test case will be printed in a separate line.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 20
1 <= N, R, G <= 10 ^ 4
0 <= u, v <= N - 1
Time Limit: 1 sec.
```

SIMILAR PROBLEMS

Valid Arrangement of Pairs

Posted: 28 Jan, 2022

Difficulty: Hard

Valid Arrangement of Pairs

Posted: 28 Jan, 2022

Difficulty: Hard

Valid Arrangement of Pairs

Posted: 28 Jan, 2022

Difficulty: Hard

Left Right Print

Posted: 9 Jul, 2022

Difficulty: Moderate

COUNT ISLANDS

Posted: 14 Sep, 2022

Difficulty: Moderate

The Summit

Posted: 15 Sep, 2022

Difficulty: Easy

Distance to a Cycle in Undirected Graph

Posted: 7 Nov, 2022

Difficulty: Moderate

Popular Interview Problems: