Why author's solution of DISHOWN problem showing TLE

I am trying to understand the solution of DISHOWN problem through the author’s solution.

I fully understand the logic through code. But when I was trying to submit the same code it is showing TLE…

Please help.

This is my submitted code. I just copied and pasted the author’s solution.

U forgot the part where he mentioned path compression heuristics. Path compression is really important in this problem. I had a similar problem during the contest but after applying path compression i got AC :). Hope this helps. U can check my submission if u want pointers on how to implement path compression , altough i would prefer if u googled and learnt it on your own