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.
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.
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.
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!
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.
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
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?
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)
The runtime error is due to stack overflow. Apparently the stack size parameter for Java has been reduced, I’m not sure why
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.
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.
“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.