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: NZdlki - Online C++ Compiler & Debugging Tool - Ideone.com

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 7oypdZ - Online C++0x Compiler & Debugging Tool - Ideone.com.

1 Like

corrected code EZgzVr - Online C++0x Compiler & Debugging Tool - Ideone.com

Silly mistake my bad xD!!
Btw thanks!!