Failing all test cases

Sample testcase is passed successfully.

Refer for code->

Hello @ambuj_0909,

  1. Your code is failing for Isolated node in the graph:
    Example:
    1
    4 2 2 1
    1 3
    3 2
    Expected Output:
    6 (two temples and two road, 4 is isolated)

  2. Also, As per the constraints the cost can be greater than the range of int.
    So, use appropriate data type.

Approach:

  1. if a<=b, then create a temple in each city.
    and print a*n.
  2. Now create a array/list or map for visited nodes of size v+1
  3. Loop for each node: i=1 to v //To keep track of isolated nodes also.
    taking i as source node,
    do BFS/DFS if visited[i]==false
  4. during BFS/DFS:
    make a tample at source node and
    build a rode for each city where we can reach from the source and mark these nodes as visited.

Hope, this would help.
Give a like if you are satisfied.

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.