Problem of the day
If N = 2 and prerequisite = [[1, 2]]. Then, there are a total of 2 courses you need to take. To take course 1 you need to finish course 2. So, it is possible to complete all courses.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two space-separated integers βNβ and βMβ representing the total number of courses and the total number of prerequisites, respectively.
From the second line of each test case, the next M lines represent the prerequisites for courses. Every line contains two single space-separated integers representing a prerequisite pair.
For each test case, the only line of output will print βYesβ if you can complete the courses. Else print βNoβ.
Print the output of each test case in a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 5000
0 <= M <= min(5000, (N * (N - 1)) / 2)
1 <= prerequisites[i][0] <= N
1 <= prerequisites[i][1] <= N
Time Limit: 1 sec
1
3 2
1 2
2 1
No
There are a total of 3 courses you need to take. To take course 1 you need to finish course 2. To take course 2 you need to finish course 1. So, it is impossible to complete all courses.
2
4 0
4 2
1 2
2 3
Yes
Yes