Religious people

give me the approach to solve this problem and also explain it plzz

Hey @ajayrajsingh_817
Case 1: when A<=B, then it is always beneficial to simply build temple in every city!
Case 2: when A>B, here you can build temple in a city and then make as many roads such that maximum possible cities are connected to this city!
If you see carefully, it becomes a connected components problem!
Try to solve it now


correct my code not passes any test cases

try this question using union find algorithm but only two test case pass


kindly correct my code in both approach
dout of first approach posted above

Here is your debugged code

For DSU approach

sir in dsu cost += a + (sz[i]-1)*b;
in dfs ans += a + (count-1)*b;
plzz explain this

cost for a connected component having x nodes = a+(x-1)*b, (as you make temple in one city and connect remaining x-1 cities by road)