Is my logic correct?


I have built a temple for every city which is not connected to any other city.
Then i have sorted all the cities based on their number of roads and built temple in the one with the maximum roads and marked all its neighbours.
Still I am getting both wrong answer and TLE in different cases

You are slight wrong.
if cost to build temple <= repairing road then i am building temples in all the cities
and if this is not the case then i am counting no of trees present in forest of graph and then building one temple in each component and then repairing the roads for all the remaining cities…

i have already included the case for all temples

https://ide.codingblocks.com/s/286664 Have a look at this code for reference.