

For given N = 4, M = 4,

In the above example, the number of nodes reachable from nodes 0 , 1, 2 and 3 is 4.
The first line contains one positive integer ‘T’, denoting the number of test cases, then ‘T’ test cases follows.
The first line of each test case contains two integers ‘N’ and ‘M’, denoting the number of nodes and the number of edges.
The next ‘M’ lines of each test case contains two space-separated integers ’u’ and ‘v’, denoting the edge between ‘u’ and ‘v’.
The first line of each test case contains an ‘N’ space separated integer, denoting the number of nodes reachable from node ‘i’, where 0 <= ‘i’ <= ‘N’ - 1.
Output of each test case will be printed on 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, M <= 10 ^ 3
0 <= u, v <= N - 1
Time Limit: 1 sec.
The idea is very simple: we will perform dfs from each node and count the number of nodes in that particular component.
The steps are as follows:
2D array ‘Graph’, to store graphs.
‘Visited’ array to mark each node whether it is visited or not.
Let ‘countNodes(n, m, edges)’ be the function that counts the number of nodes reachable from each node. It returns the array of size n.
The idea is very simple: we will perform a single dfs and keep track of the component number to which each node belongs and the count of the number of nodes in each component.
The steps are as follows:
Let ‘countNodes(n, m, edges)’ be the function that counts the number of nodes reachable from each node. It returns the array of size n.