Problem LinkSetter: Hasan Jaddouh DifficultyEASYMEDIUM PrerequisitesStrongly Connected Components, Bfs / Dfs ProblemFind the minimum number of cells to select in a matrix so that we can reach every cell from it through a series of moves which is, "You can move from one cell to another only if they are adjacent (i.e. share a side) and the height of destination cell is not more than height of current cell." ExplanationThe edges in the graph will be between cells $(i, j)$ and $(x, y)$, only if they share a side. Also, the edges will be directed as the edge exists from a cell into another only if the height of destination cell is not less than the height of the current cell. For example in the sample input 1, the edges will be as follows: Now given a directed graph, we want to select a set of cells so that all cells are reachable from it. The first observation is 2 decompose the graph into Strongly connected components, as there may be cycles in the graph. This is because all the vertices in a cycle can be visited by starting from any vertex. Now, we have a directed graph in which there are no cycles. (It is also called a DAG). In this graph, we claim that we chose only vertices with indegree $0$ to be in our set as the possible candidate solution. Reason : Let us say there is an edge $P$ to $Q$ in the DAG. Let us denote the set of vertices reachable from $Q$ to be $S$. Then all vertices in $S$ are also reachable from $P$. But $P$ is not reachable from $Q$. So continuing this process, we will see that only vertices having indegree $0$ are not reachable from any other vertex but they as a set cover all the vertices reachable from it. The DAG of input sample 1 is give below: In the above figure, $1$ represents the set ${1}$, $2$ represents the set ${2,3}$, $3$ represents the set ${4,7}$ and $4$ represents the set ${5,6,8,9}$. Thus, the algorithm is as below:
Author's SolutionThe solution counts the number of components having same heights, such that all adjacent components are less (strictly) than it. Proof: Each such component consisting of all cells of same height can be represented by 1 cell only. Now, each component can either have all adjacent components less tha it or not. If all cells adjacent to it are smaller, the those can be reached from the current cell, so they are ot counted towards the answer. If we repeat this process, for each component, we are bound to find only those cells which will contribute to the answer. Also, if we assume some cell that shouldn't be in answer was counted by above process, the it would contradict the fact that all adjacent components are lesser than it. The above process can be simply implemented by a dfs call from every vertex and check if all components adjacent to it are smaller than it or not. For details, refer to author's solution. Time Complexity$O(N * M)$, per test case. Space Complexity$O(N * M)$ Solution Links
This question is marked "community wiki".
asked 19 Nov '17, 21:27

My solution was to begin from the cell whose height is maximum and finding all the cells which can be visited by using simple dfs from that cell, and continue this process until all the cells are visited. Thoughts behind this greedy approach : You cannot visit the cell with max height from any other cell whose height is less than it, and can be visited only by using cells with same height so in order to visit this cell you can start from any cell with same max height and do this process continuously. Refer to this implementation for more details. answered 20 Nov '17, 09:13

Solution with DFS for this question gives me runtime error. But this works fine with BFS. Same thing happend during September Long challenge with FILLMTR question. Is here anyone who solved this question with DFS in JAVA? Thanks in advance. answered 20 Nov '17, 02:24
3
The runtime error is due to stack overflow. Apparently the stack size parameter for Java has been reduced, I'm not sure why :/
(20 Nov '17, 06:10)
1
I have submitted recursive DFS on Codeforces upto depth 10^6. It works fine there. I think only solution is to do BFS on nonrecursive DFS on Codechef.
(20 Nov '17, 13:20)

@chef_keshav faced same problem.. DFS accepted in cpp. giving NZEC in java.. answered 20 Nov '17, 02:55

Regarding implementing DFS in JAVA: It seems as though implementing DFS recursively will result in a NZEC. The JAVA wiki suggests implementing DFS with a stack instead to prevent this problem. I did as suggested and got the answer correct. My solution. If anyone has managed to implement DFS recursively in JAVA please include your solution. answered 20 Nov '17, 03:02

My implementation is much easier but complexity is more. Still got AC in 0.12s.
answered 21 Nov '17, 16:46

I dont like this. They should have reduced the constraints. My same solution with c++ got AC but NZEC in java. And i had no clue why i was getting NZEC. answered 20 Nov '17, 06:54

Hi there, my Python implementation (committed after the competition ended) uses something like BFS, not recursive. I receive NZEC when I submit, but it works fine in my tests, even the one with 1000x1000 of 10**9 height as input. I couldn't figure out why it's crashing. Can anybody help me, or suggest an input that will cause it to crash? Thank you! Note: I know there's a logic mistake there. I didn't fix it because I was too busy looking for the cause of NZEC. In the 4th test case example I showed that it produces an incorrect result, but does not crash, so I'm still lost. Thank you all in advance :) answered 20 Nov '17, 06:42

Hi guys, I know this might be a silly question but my submission gets TLE and I cant figure out why...I have implemented BFS in C++ , almost identical to the Editorialist's solution. Can anyone help me? Code here ... Thanks in advance! answered 20 Nov '17, 23:06
you first mark vertices as visited and then push them into stack, this is important when there are 2length cycle in graphs. See, my exact code for details.
(21 Nov '17, 00:51)
Yes you're right , i didn't notice this .. Thanks for the help!
(21 Nov '17, 01:59)

How did you draw the dag for the above first sample input?Is there any way to draw the dag for any graph? answered 21 Nov '17, 10:07
No, i use draw.io and graphonline.ru/en/ for help, along with paint.
(21 Nov '17, 18:07)

Can some one tell me what is the problem here. This code works fine with the testcases but give a NZEC when I submit it. I used a DFS in java.Here's my code: https://www.codechef.com/viewsolution/16348375 answered 22 Nov '17, 21:54

can anyone plzz tell me in order to solve this question i have to learn KOSARAJU'S ALGO or not , i know nothing about SCC and very weak graph thory(i.e BFS and DFS) answered 28 Nov '17, 21:09
1
Hi, you can also solve it gredily also. See above comments for help.
(28 Nov '17, 23:39)
Can u plzz explain again where the concept of SCC is being implemented in this problem or if without SCC how to solve it ?
(29 Nov '17, 00:10)
1
@coder_ishmeet, SCC is used to find the directed acyclic decomposition of graph as mentioned in editorial. The other approach without SCC is mentioned in author's solution and comments above. like this one
(29 Nov '17, 18:31)
ohkay thnkss alot...... i will solve this question with or without SCC both
(30 Nov '17, 14:59)

I am sorry that I can't comment at the top, but I want to add on to @chef_keshav and @meooow comments that this soln in JAVA works fine with dfs. answered 17 Aug '18, 00:45

I think the link to the tester's solution is wrong. It is again redirecting on this page.