Please read the new rule regarding the restriction on the use of AI tools. ×

Need help in solving a SPOJ problem

Revision en1, by Awwwwwsheshhhh.., 2022-07-15 20:10:21

I am having some trouble with the famous SPOJ problem : AKBAR

Hashtags : bfs,dfs,graphs

The method I used was : For every city I checked if there was a soldier present (initially) or not, if there is one, I used bfs from that city to all connected cities possible under the range of the soldier's power. To check for connected graphs, I maintained another vis2 array to ensure the same soldier was not visiting parent nodes ( just an optimization ). I also used another vis array to check if a city is guarded by 2 or more soldiers (in which case I had to return NO) and 2 queues to run the bfs.

This is my code : Link

This is not the most efficient in terms of time or memory complexity, and I totally understand that. However SPOJ returns WA for the code. I have gone through all edge cases mentioned in comments over all sites possible ( SPOJ itself, Codechef, Codeforces (yes, there is a blog made previously on this problem and C137 did help a lot there) but I can not find a testcase which would fail for my code.

I was disturbed by some comments on SPOJ which say that correct solutions are failing ( or something of that sort ), so I am writing this blog. Could you please help me find the error in my code ? Thanks in advance :)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Awwwwwsheshhhh.. 2022-07-15 20:10:21 1403 Initial revision (published)