

You are given ‘N’ = 5, ‘GRAPH’ = [[1, 1, 1, 0, 0],[1, 1, 0, 0, 0],[1, 0, 1, 0, 0],[0, 0, 0, 1, 1],[0, 0, 1, 0, 0]], ‘M’ = 2 and ‘INFECTED’ = [2 ,4].

If we remove computer number 2 from infected computers, we can prevent computers numbers 0 and 1 from getting infected. Then total infected computers will be 2(3 and 4). If we remove computer number 4 from infected computers, we can prevent computer number 3 from getting infected. Then total infected computers will be 3(0, 1, and 2). Hence, it’s better to remove computer number 2 from infected computers.
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, representing the number of the computers and the number of initially infected computers, respectively.
The next line ‘N’ lines contain ‘N’ space-separated integers representing the computer network.
The next line contains ‘M’ space-separated integers representing the infected computers.
For each test case, print the index(0-indexed) of the computer to be removed to minimize the spread of the malware.
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
2 <= N <= 2000
1 <= M <= N
0 <= GRAPH[i][j] <= 1
GRAPH[i][j] == GRAPH[j][i]
GRAPH[i][i] = 1
0 <= INFECTED[i] < N
INFECTED contains unique elements.
Time Limit: 1 sec
In this approach, we will color each component with color using a depth-first search. Each connected component will have a different color. Now, we can observe that if two computers in ‘INFECTED’ have the same color, removing any one of them won’t decrease the spread because the other can still infect the whole component. So, we will remove a computer that satisfies the following properties:
If there exists more than one such computer, we will remove the one with the lowest index.
Algorithm :
Maintain a function DFS(int[][] ‘GRAPH’, int[] ‘COLORS’, int ‘V’, int ‘COLOR’)
In approach 1, we have seen that if we know the size and the computers present in each connected component, we can easily solve this problem. So, In this approach, we will use a disjoint-set or union-find data structure to store the collection of disjoint components.
We will store the size of each component. Whenever we union two components together, the size of those components will be added.
So, we will remove a computer that satisfies the following properties:
Algorithm :
Maintain a function ‘FIND’(int ‘X’).
Maintain a function ‘UNION’(int ‘X’, int ‘Y’).