TLE in FOXLINGS SPOJ
for(int i=1; i&lt;=n; i++){
    findParent(dj, i);
}

for(int i=1; i&lt;=n; i++){
    if(!map.containsKey(dj[i])){
       count++;
        map.put(dj[i], 1);
    }
}


This part is slow and unnecessary - you can count how many times you did union instead. 

 int[] dj= new int[n+1];


With 10^9 limit for N, only that line requires 4GB of RAM, when limit for task is 1.5GB. Use HashMap instead and store only unioned ids - there will be at most 2*M of those.