Bloomberg Interview Question

a hard leet code question about graphs/trees