‘N’ children are going home after their school break. Their roll numbers are assigned from 1 to ‘N’. There is a shop along the path to home from which they buy chocolates. You are given an array ‘CHOCOLATES’, representing the number of chocolates ‘ith’ child wants but the shop only has ‘C’ chocolates. Children can only take chocolate from that shop if the following two conditions are satisfied:
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.
There are ‘M’ pairs of friends among the children. Find the maximum number of children who can take all the chocolates they want?
Note:If ‘A’ is a friend of ‘B’, then ‘B’ is also a friend of ‘A’.
Example:
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.
Output Format :
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.
Note :
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
1
4 3 4
1 2
2 3
2 4
1 1 1 1
4
Child 1 is a friend of child 2, who is the friend of both children 3 and 4. So if child 1 takes chocolate, shops need to have 4 chocolates. So the maximum count of students that will take chocolates is 4.
1
3 1 4
1 2
3 2 2
1
Child 1 is a friend of child 2. So 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).
Think of friendship as a graph.
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).
O(2^N), where ‘N’ is the number of children.
The total number of recursive calls for a vertex can be maximum up to N in which we will traverse every vertex and edge. The total number of vertices is ‘N’, and the total number of edges is ‘M’. For each connected component, we have two choices whether to include or not, which give a 2^N number of options. Therefore, the overall time complexity will be O(2^N).
O(N + M), where ‘N’ is the number of children, and ‘M’ is the number of connections between friends.
For each vertex, we store its adjacent vertices; therefore, storing them in the form of a graph will require O(N + M) space. We also use the ‘VISITED’ array, which all require O(N) space. The recursive stack of the DFS function will contain all the neighbors of a vertex, which at most can be ‘N’ vertices. Recursion stack for HELPER function can have at most ‘N’ number of calls. Therefore, the overall space complexity will be O(N + M).