Medium-hard BFS + cycle detection.