Citadel interview question

Disconnected graph problem, leetcode style