I was trying to solve this problem. I solved it using bfs. However, I would like to know how can I generate the whole graph beforehand — before processing it in dfs or any other algorithm- ? Another problem that I would like to employ this technique on is problem A in this contest