Minimize cost to connect the graph by connecting any pairs of vertices having cost at least 0 | C # program for the above approach ; Function to perform the find operation to find the parent of a disjoint set ; FUnction to perform union operation of disjoint set union ; Update the minimum value such than it should be greater than 0 ; Update the minimum value such than it should be greater than 0 ; Function to minimize the cost to make the graph connected as per the given condition ; Stores the parent elements of sets ; Stores the rank of the sets ; Stores the minValue of the sets ; Update parent [ i ] to i ; Update minValue [ i ] to cost [ i - 1 ] ; Add i . first and i . second elements to the same set ; Stores the parent elements of the different components ; Insert parent of i to s ; Stores the minimum value from s ; Flag to mark if any minimum value is negative ; If minVal [ i ] is negative ; Mark flag as true ; Update the minimum value ; Stores minimum cost to add the edges ; If the given graph is connected or components minimum values not having any negative edge ; Update the minCost ; Print the value of minCost ; Print - 1 ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int [ ] minVal , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( rank [ a ] == rank [ b ] ) rank [ a ] ++ ; if ( rank [ a ] > rank [ b ] ) { parent [ b ] = a ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ a ] = Math . Min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ a ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ a ] = minVal [ b ] ; } else { minVal [ a ] = Math . Max ( minVal [ a ] , minVal [ b ] ) ; } } else { parent [ a ] = b ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ b ] = Math . Min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ b ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ b ] = minVal [ b ] ; } else { minVal [ b ] = Math . Max ( minVal [ a ] , minVal [ b ] ) ; } } } static void findMinCost ( pair [ ] G , int [ ] cost , int N , int M ) { int [ ] parent = new int [ N + 1 ] ; int [ ] rank = new int [ N + 1 ] ; int [ ] minVal = new int [ N + 1 ] ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = i ; minVal [ i ] = cost [ i - 1 ] ; } foreach ( pair i in G ) { Union ( parent , rank , minVal , i . first , i . second ) ; } HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 1 ; i < N + 1 ; i ++ ) { s . Add ( Find ( parent , i ) ) ; } pair min = new pair ( 0 , int . MaxValue ) ; bool flag = false ; foreach ( int i in s ) { if ( minVal [ i ] < 0 ) { flag = true ; } if ( min . second > minVal [ i ] ) { min = new pair ( i , minVal [ i ] ) ; } } int minCost = 0 ; if ( ! flag || ( flag && s . Count == 1 ) ) { foreach ( int i in s ) { if ( i != min . first ) { minCost += ( minVal [ i ] + min . second ) ; } } Console . Write ( minCost + " STRNEWLINE " ) ; } else { Console . Write ( - 1 + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int N = 6 ; pair [ ] G = { new pair ( 3 , 1 ) , new pair ( 2 , 3 ) , new pair ( 2 , 1 ) , new pair ( 4 , 5 ) , new pair ( 5 , 6 ) , new pair ( 6 , 4 ) } ; int [ ] cost = { 2 , 1 , 5 , 3 , 2 , 9 } ; int M = G . Length ; findMinCost ( G , cost , N , M ) ; } }