can anyone please tell me which theorem is to be applied in connecting islands??
The question is basically asking if two vertices belong to the same connected component( edges are added dynamically here though).
Union-Find Disjoint data structure can be used to solve this problem. For each edge to be added between x any y, take the Union of set(x) and set(y) .
For checking if vertices x and y are connected or not, see if they both belong to the same set.