Unable to get solution for CodeForces DSU Problem C

This is the problem link Problem link

I am confused and unable to get the solution to the problem, please help me to solve this problem.
I have tried this, My Solution. But it’s giving WA. Please help