Pregunta de entrevista de Publicis Sapient

basic algorithmic question, based on dfs.