Religious people(please tell the correction)

Hello @Saksham12,

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 iolated)
Your Output is:
4
Reason:
Because you are taking 1 as source node for dfs.
Solution:
Rather take all nodes as source node one by one.

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.