Is my approach and thought process correct for this?

Count the number of disjoint components (subgraphs which are not connected to each other) and store them in a variable , say ‘components’
Each component must have a single Temple
So cost of temple in each component is A * components
Further there are (x-1) roads required in each component where x is no of vertices in that component
Sum of roads in all components = (n - components) , n is no of nodes in adjacency list
Multiply that with B , cost to repair each road
Cost so far , cost = A*component + B * (n-components)

But there are some vertices that do not form any edges Some vertices which stand alone and each of these vertices must have a temple of its own Since they do not form any edge , there count is not included in the adjacency list No of such nodes is (V - n) …where V is total no of nodes and n is no of nodes that form an edge and are part of previously counted components Since each of these nodes must have a temple Total cost = Acomponents + B(n-component) + A*(V-n)

Do not forget to consider the base case when A<=B. If cost of constructing temples in each city is less than repairing roads , simply build temples in all cities and return V*A

ur code is missing this part
But there are some vertices that do not form any edges Some vertices which stand alone and each of these vertices must have a temple of its own Since they do not form any edge , there count is not included in the adjacency list No of such nodes is (V - n) …where V is total no of nodes and n is no of nodes that form an edge and are part of previously counted components Since each of these nodes must have a temple Total cost = A components + B (n-component) + A*(V-n)

ans += (v-l.size())*m; writing this will help ? …check the updated code once

okay… let me see yr code

in line number 63 …

ur code seems perfectly fine… i dont understand why its failing test cases… let me try it on some other inputs

??? you got anything

Actually there is problem from backend that’s why I am unable to access test cases… but ur codes seems fine to me

ok please check with your team

ok… so i found the error
image
here in x-1 brackets were missing thats why all test were not passing. add brackets and try submitting it will pass all test cases

shitttttt…thank you

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.