The algorithm is not precise : how do you maintain the visit array ? The main loop should be described.