Coding Ninjas is offering lots of courses. But for enrolled in courses, there may be some prerequisites according to the course. So lots of queries are coming for confirming the sequence, of course, to confirm that the sequence of taking the course is correct or not.
So help our ninja to write a code so he can give an answer to the queries whether the sequence is correct or not.
So your task is to answer each query whether it is a correct sequence or not. You will be provided with the total number of courses 'N', a list of direct prerequisite pairs, and a list of query pairs.
Example:In the image left side represents the prerequisite and on-right queries.
consider the following example suppose the number of courses ‘N = 2’prerequisites pair is [ [ 1, 0 ] ] and queries are [ [ 0, 1 ], [ 1, 0 ] ] so we return [ False, True ] as ‘0’is not a prerequisite for the course ‘1’but ‘1’is prerequisite for the course ‘0’.
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, the total number of courses.
The second line of each test case contains an integer ‘M’ and ‘Q’ denoting the number of rows in the array ‘PREREQUISITE’ and ‘Q’ rows in array queries.
Then, ‘M’ lines follow:
Each line contains ‘2’ space-separated integers denoting the row values.
Then, ‘Q’ lines follow:
Each line contains ‘2’ space-separated integers denoting the row values.
Output Format :
For each test case, print ‘True’ if the order is correct else ‘False’.
Print output of each test case in a separate line.
Note:
You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
1 <= T <= 10^2
1 <= N <= 3*10^3
0 <= PREREQUISITE[i] <= 3*10^3
Where ‘T’ is the total number of test cases, ‘N’ is the number of courses and 'PREREQUISITE[i]' is the value of that courses.
Time Limit: 1 sec
2
3
2 2
1 0
2 0
0 1
2 0
5
4 4
1 2
0 1
2 3
3 4
0 4
4 0
1 3
3 0
False True
True False True False
Test Case 1:
In this image left side represents the prerequisite array and the right side represents the query array.
So according to this test case, the number of courses is ‘N = 3’ prerequisite array is [ [ 1, 0 ], [ 2, 0 ] ] and queries array is [ [ 0, 1 ], [ 2, 0 ] ] so we return ‘False’ and ‘True’ as ‘0’ is not prerequisite for ‘1’ course and 2 is prerequisite for the course ‘0’.
Test Case 2:
In this image left side represents the prerequisite array and the right side represents the query array.
So according to this test case, the number of courses is ‘N = 5’ prerequisite array is [ [ 0,1 ], [ 1, 2 ], [ 2, 3 ], [ 3, 4 ] ] and queries array is [ [ 0, 4 ], [ 4, 0 ], [ 1, 3 ], [ 3, 0 ] ] so we return ‘True’ for the first query and the third query as ‘1’ is indirectly prerequisite for course ‘4’ as well as for course ‘3’.and we return ‘False’ for the second and fourth query as ‘0’ is prerequisite for course ‘3’ and ‘4’ not ‘3’ and ‘4’ are prerequisite for the course ‘0’.
2
3
3 2
1 2
1 0
2 0
1 0
1 2
3
2 2
1 0
2 0
0 1
2 0
True True
False True
Can you iterate over the courses?
We can think of solving this problem like a graph where we can link courses with a directed edge if the prerequisite is needed and now we can check if it is possible or not for each query. Like we make nodes with the value of courses and join them in the form of the directed graph if the prerequisite is needed.
O(N^3), where ‘N’ is size of the array.
We are iterating a three-nested loop of size ‘N’ to fill the value of ‘DP’ array. Hence the time complexity is O(N^3).
O(N^2), where ‘N’ is the size of the array.
As we are declaring a ‘DP’ array of size ‘N’ ^ 2. Hence the space complexity is O(N^2).