Pregunta de entrevista de Bloomberg

a hard leet code question about graphs/trees