TLE IN A HACKEREARTH QUESTION

I am solving this problem of dsu I know the approach but getting tle in some of the test cases!!
I checked the Editorial they have used the same approach!!
Can anyone explain why I m getting TLE!!

my code link: https://ideone.com/NZdlki

THANKS IN ADVANCE!!

sz[q]=0;
sz[p]+=sz[q];
???
u are not updating the size of parents group and also before updating your answer check if it is present in map or not.
corrected code https://www.ideone.com/7oypdZ.

1 Like

corrected code https://www.ideone.com/EZgzVr

Silly mistake my bad xD!!
Btw thanks!!