SKIING - Editorial

Problem Link


Setter: Hasan Jaddouh
Tester: Amr Mahmoud
Editorialist: Bhuvnesh Jain




Strongly Connected Components, Bfs / Dfs


Find 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.”


The 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:

  1. Construct the directed graph from the grid. Complexity is $O(N * M)$. Also, the number of edges in the graph is bounded by $(8 * N * M)$ as each cell can have at most 4 neighbors and it can have edge in both directions.
  2. Decompose the graph into one with no cycles. You can use Tarjan's algorithm or Kosaraju algorithm for this step.
  3. Find the number of vertices with indegree $0$ in DAG.

Author’s Solution

The 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

Setter’s solution
Tester’s solution
Editorialist’s solution


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.


@chef_keshav faced same problem… DFS accepted in cpp. giving NZEC in java…


@chef_keshav & @nik_84

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.


The runtime error is due to stack overflow. Apparently the stack size parameter for Java has been reduced, I’m not sure why :confused:
Here is a submission I made just now with a simple recursive function upto depth 105: 16323256. It gives RE.
And here is another submission I made in January of this year where recursion goes upto 106 without any error: 12408377.


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 :slight_smile:

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.


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.


I have submitted recursive DFS on Codeforces upto depth 10^6. It works fine there.

I think only solution is to do BFS on non-recursive DFS on Codechef.

1 Like

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!

you first mark vertices as visited and then push them into stack, this is important when there are 2-length cycle in graphs. See, my exact code for details.

Yes you’re right , i didn’t notice this … Thanks for the help!

How did you draw the dag for the above first sample input?Is there any way to draw the dag for any graph?

My implementation is much easier but complexity is more. Still got AC in 0.12s.

I have made a structure having height, row and col for each cell. Then I have sorted vector of structures according to height(decreasing order) to get the cell which has max height first. Then if the cell is not previously visited then I have applied DFS on that cell.

Time Complexity O(mnlg(mn))

Although during contest I have solved in O(mn)

I hope you like it. :slight_smile:


No, i use and for help, along with paint.

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

1 Like

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:

Read the comments and answers here lol…

in Editorialist’s solution why has been two array dx[4] = {-1, 1, 0, 0} and dy[4] = {0, 0, -1, 1} used and sort(vals.rbegin(), vals.rend()) sorts which part of the vector?

“dx” and “dy” array together form the possible adjacent vertices in grid while “sort(vals.rbegin(), vals.rend())” sorts the given array in descending order.