


Sometimes a removal may refer to a location with no stone, in that case, no stone fall.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains three space-separated integers, 'N', 'M' and ‘R’, denoting the number of rows and columns in the matrix 'GRID’', respectively and ‘R’, denoting the number of the location he has to remove.
Each of the next 'N' lines contains 'M' space-separated integers(0s or 1s) denoting the matrix ‘GRID' elements.
Next ‘R’ lines contain 2 integers denoting the removal of stone placed at ( i, j ) position in the 'GRID'.
For each test case, print a single line containing space-separated integers denoting the number of stones that fall after each removal of a stone.
The output for each test case is printed 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, M <= 200
1 <= R <= N * M
Where ‘T’ is the number of test cases, ‘N’ and ‘M’ is the number of rows and columns respectively and ‘R’ is the number of removal operations.
Time limit: 1 sec.
The idea here is to use disjoint set union (DSU) to solve this problem.
DSU is data structure provides the following capabilities. We are given several elements, each of which is a separate set. A DSU will have an operation to combine any two sets, and it will be able to tell in which set a specific element is.
First, we will be going to remove all the stones that are given in the removal array and then one by one we add those stones in the grid back but before adding the stones back we make different sets where each set represents all the connected stones.
Then we iterate the removal array from reverse and whenever we find a stone, we connect all the adjacent stones (set will be connected) and we are also storing the size as the sets get connected.
And if a set is connected to another set that already has a stone that is connected to the top then we add this value to the result.
Algorithm:
Description of union_set( int a, int b)
Description of Find_set(int v)
Group Points
Group Points
Group Points
Group Points
Check Equations
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Ninja And The Malicious Software
Good Bad Graph
Good Bad Graph
COUNT ISLANDS