Let‘N’ = 3 , ‘K’ = 1 and ‘DEPENDENCIES’ = [ [1, 2] [2, 3] ].
In this example, if you want to take ‘COURSE_3’ then first you have to take ‘COURSE_2’ in the previous semester. If you want to take ‘COURSE_2’, then first you have to take ‘COURSE_1’ in the previous semester.
The value of ‘K’ is 1 which means in a semester we can choose at most 1 course.
The first line of input 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 three single space-separated integers ‘N’,‘M’ and ‘K’ represent the number of courses, number of dependencies and maximum courses that you can take in one semester respectively.
The next ‘N’ line of each test case contains two single space-separated integers ‘X’ and ‘Y’ representing that the course ‘X’ must be taken before the course ‘Y’.
For each test case, print the minimum number of semesters required to take all the courses.
Print the output of each test case in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 15
0 <= ‘M’ <= ‘N’ * (‘N’ - 1)/2
1 <= ‘X’, ‘Y’ and ‘K’ <= ‘N’
Time Limit: 1 second
As we know we can only take a course which is not dependent on any other course. So first we make a graph having edges ‘DEPENDENCIES[i]’ and nodes represent each course. Then we find all the nodes whose in-degree is 0 i.e. these nodes don’t depend on any other nodes. If the nodes are less than ‘K’ then we take all the nodes and subtract the indegree of all the nodes which are connected through these nodes. Else we are trying to select ‘K’ nodes by all possible ways from the current set of nodes and find our answer.
Here is the algorithm:
minSemHepler(‘MASK’)
NINJA AND HAPPINESS
DECODE STRING
COUNT ISLANDS
Distance to a Cycle in Undirected Graph
Randomly Sorted