New update is available. Click here to update.

Last Updated: 29 Mar, 2021

Difficulty: Hard

```
There is no limitation on taking the number of courses in a particular semester as long as all the prerequisites for taking the course are satisfied.
```

```
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line of each test case contains two integers ‘N’ and ‘M,’ which denotes the number of courses and the number of rows of the matrix ‘prerequisites.’
The next M lines contain two integers, prerequisites[i][0] and prerequisites[i][1], denoting that prerequisites[i][0] has to be studied before prerequisites[i][1].
```

```
For each test case, print the minimum number of semesters required to study all the courses.
Print the output of each test case in a separate line.
```

```
1<= T <= 50
1 <= N <= 20000
0 <= M <= 20000
1 <= prerequisites[i][0], prerequisites[i][1] <= N
prerequisites[i][0] != prerequisites[i][1], for any valid i
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: