Citadel Interview Question

Disconnected graph problem, leetcode style