
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.
For each test case, print ‘True’ if the order is correct else ‘False’.
Print output of each test case in a separate line.
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
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.
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.