Problem of the day
If it is impossible to finish all courses, return an empty array. If there are multiple answers, return any one.
The first line of input contains an integer 'T' representing the number of the test case.
The first line of each test case contains an integer ‘N’ representing the number of courses.
The second line of each test case contains a given integer ‘M’ representing the number of prerequisite pairs.
The next ‘M’ lines in each test case contain a matrix ‘PREREQUISITES’ containing two integers denoting a prerequisite pair.
For each test case, print a single integer 1 if the returned order of the courses is correct otherwise we return 0.
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
2 <= N <= 100
1 <= M <= min(100,N(N-1)/2)
1 <= PREREQUISITES[i][0], PREREQUISITES[i][1] <= N
Where ‘PREREQUISITES’ denotes the prerequisites matrix.
Time limit: 1 sec
2
4
4
2 1
3 1
4 2
4 3
3
2
1 2
2 3
1
1
In test case 1, There are a total of 4 courses to take. To take course 4 you should have finished both courses 2 and 3. Both courses 2 and 3 should be taken after you finish course 1. So one correct course order is [1,2,3,4]. Another correct ordering is [1,3,2,4]. When the ordering is one of the above two sets then output is 1.
In test case 2, There are 3 courses to take. To start with, First course 3 is taken. Then course 2 is taken for which course 3 must be completed. At last course 1 is taken for which course must be completed. So correct course order is [3,2,1].
2
2
1
2 1
1
0
1
1
In test case 1, there are a total of 2 courses to take. To take course 2 you should have finished course 1. So the correct course order is [1, 2]. Ordering [1, 2] is correct.
In test case 2, there is only one course and so no pairs of courses are possible and thus the only correct order of courses is [1].