First I tried putting each zero as one in matrix and computing for each zero the maximum size of group.but this method shows TLE.
then I read this solution… https://ide.codingblocks.com/s/77475
but still I m not able to understand it… so explain me this solution approach more in detail…
NOT ABLE TO GET THE LOGIC OF SOLUTION
hey, use this approach, it is same as in the solution, group id is parent, black is 1, white is zero,
in first mark all the regions by their size
suppose
1 1 0
0 0 1
0 0 1
mark it as
2 2 0
0 0 2
0 0 2
these are the size of the ponds also mark them visited while iterating so u do not iterate over the same group again
and using DSU mark the parent of each cell
now iterate over the matrix again
only look for zeroes
once u find a zero
look for the elements adjacent to and pick the value of the elements that have diff parents add them and add 1 to it, to include this zero as well
this will solve your question
if you’re getting a TLE then that is probably because you’re making same calculations again and again.
I guess your solution might be to iterate over all the cells and find the one which maximize the size, when you do this you’ll have to check the size of the pond again and again and this is causing the TLE.
What you can do is that you could mark the cells with the size of the pond that they’re connected to so you won’t have to run a complete dfs again and again. Think about it.