Difference in the approach

I tried solving this using union find algorithm.

I got TLE in one and AC in second approach. I made only one line difference between the two and both are giving correct answer on my laptop. Can anyone tell me why this happened?

TLE

AC