Hi, I am trying to solve Religious People problem from graph section but could not pass all test cases

My code is - https://ide.codingblocks.com/s/197506

My approach is if b>=a then ans will be simply (a*b)
else if (a>b) then I have stored indegree of all nodes in a set. I am finding node with maximum indegree build a temple there and repair roads connected to it. I am able to pass 1 test case in submission. I want to know if this approach is correct or not. If this approach is correct then where I am making a mistake.

Hello @gourav,

There are following problems in your code:

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

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 if visited[i]==false

  4. during BFS:
    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.

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.

I tried to open “rate experience” page but it says page could not be found.

Hello @gourav,

It’s okay. I have myself marked it as resolved so you won’t get that message anymore.