Segmentation fault is coming

Hey @Vipul.kapoor

		while(r--)//r-- here
		{
			cin>>x>>y;
			x--,y--;//added this
			g.addedge(x,y);
		}

Now try to debug logically :slight_smile:

no sir not getting it
and please tell u had decremented values of x and y

Whar are u not getting ?
I decreased x and y because u considered 0 indexed graph
But in ques its 1 indexed

output of my code is not matching and not able to find logical error in my code

Heyy @Vipul.kapoor
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 = A components + 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 .

sir i have applied this approach i have counted the total no of disjoint components and have also considered the total the no of edges in disjoint set and have put them in a vector edges. the base case you mentioned above is also being considered

Hey @Vipul.kapoor
According to above u have to do this

		if(ct>cr)
		{
			ans=(no*ct+cr*(c-no));//updated this
		}
		else
		{
			ans=c*ct; //updated this 
			// for(auto it:g.edges) removed this
			// {
			// 	ans+=(it*cr);
			// }
		}

Please check ur logic again :slight_smile:
Use long long to avoid overflow and get testcases passed :slight_smile:

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.