
1. The student must be able to take all the chocolates that they want to take.
2. All their friends must be able to take all the chocolates they want to take.
If ‘A’ is a friend of ‘B’, then ‘B’ is also a friend of ‘A’.
Let the number of children be: 3
Let the number of chocolates be: 4
Let 1 and 2 be friends.
Let chocolates required by each child be: 3 2 2
Child 1 is a friend of child 2. If child 1 takes chocolate, shops need to have 5 chocolates which are greater than the actual amount present in the shop. So the maximum count of students that will take chocolates is 1 (child 3 can take chocolates as he needs 2 chocolates).
The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains three space-separated integers, ‘N’, ‘M’, and ‘C’, representing the number of children, the pair of friends, and the number of chocolates present in the shop, respectively.
The next ‘M’ lines contain two space-separated integers, ‘X' and 'Y', which signifies a friendship between ‘X’ and ‘Y’.
The ‘M’ + 1 line of each test case contains ‘N’ space-separated integers, representing the chocolates each child wants.
For each test case, print a single integer representing the maximum number of children who can take all the chocolates they want.
Print 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 <= 5
1 <= N <= 10^3
1 <= M <= 10^4
1 <= X, Y <= N
1 <= CHOCOLATE[i] <= 10^3
1 <= C <= 10^3
Time Limit: 1 sec
The basic idea is to find the number of connected components in the graph. We need to find the total sum of chocolates present in a connected component with their number of children to get the number of children present in the connected component with their total sum of chocolates present. We then need to find the maximum number of children whose sum of chocolates is smaller than equal to ‘C’.
Here is the algorithm :
DFS(‘GRAPH’, ‘U’, ‘VISITED’, ‘CHOCOLATES’)
HELPER function finds the maximum number of children that can get chocolates from the shop. For each value of ‘CHILDREN’, we have two options: whether to include the current value or not. So, we can obtain the maximum count by:
HELPER(‘C’, ‘COUNT’, ‘CHILDREN’, ‘S’) (where ‘N’ is the size of the ‘COUNT’ array).
The approach is similar to the previous approach. In this approach, we will use an array (say, ‘DP’) to store the recursive calls.
How do we know we are calling the same function more than once?
Let ‘N’ be 3 and ‘C’ be 2.
We call HELPER function on values (3, 2)
(3, 2) if we include current value then it calls helper function on values (2, 1) ->(1, 1), (1, 0) and so on.
(3, 2) if we exclude current value then it calls helper function on values (2, 2) -> (1, 2), (1, 1) and so on.
As we can see (1, 1) is called twice. For large values of ‘N’ and ‘C’ there can be many repeated recursive calls. To avoid that we can use memoization to store recursive calls.
Here is the algorithm :
HELPER(‘C’, ‘COUNT’, ‘CHILDREN’, ‘S’, ‘DP’) (where ‘N’ is the size of the ‘COUNT’ array, ‘DP’ is the array used for memoization initialized with -1, and ‘S’ is the current index).
The approach is similar to the previous approach. In this approach, we will use the bottom-up approach to find the count of children for all possible numbers of chocolates from 1 to ‘W’. ‘DP[i]’ denotes the maximum number of children having ‘i’ number of chocolates.
Here is the algorithm :
HELPER(‘C’, ‘COUNT’, ‘CHILDREN’, ‘S’) (where ‘N’ is the size of the ‘COUNT’ array).