I am stuck in this problem on Hackerrank. I guess that it is a variation of connected components problem. I guess that the connected components would have to be kept track of using Union-Find DS, but I am a bit unclear about how to do that? Can anyone guide me how to solve this?