DSU : GALACTIK [Getting WA]

I recently learned the DSU algorithm and was trying a problem from CodeChef’s easy section. I am getting WA, can you please help me in the logic building part, is my dsu implementation correct? I tried to understand from the setters solution, but could not understand. can you please help me in debugging the code?

Link to my code: https://ide.codingblocks.com/s/239362
Link to question : https://www.codechef.com/problems/GALACTIK

I am using the following algorithm

  1. Use DSU to count number of components and minimum tax of each component.
  2. If any component has all planets having negative taxes, then answer is -1
  3. Otherwise find global min tax value.
  4. Compute result = sum of minimum value of all components + (components-2)*global_min

@Avi-Kasliwal-315786729062203 hey your approach is correct but there is problem in implementation,check this out:

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.