All the testcases aren't passing. Please help

import java.io.;
import java.math.
;
import java.security.;
import java.text.
;
import java.util.;
import java.util.concurrent.
;
import java.util.regex.*;

public class Solution {

// Complete the roadsAndLibraries function below.
static long roadsAndLibraries(int n, int c_lib, int c_road, int[][] cities) {
    ArrayList<ArrayList<Integer>> adj=new ArrayList<ArrayList<Integer>>();
    long cost=0;
    for(int i=0;i<n;i++)
    {
        adj.add(new ArrayList<>());
    }

    for(int i=0;i<cities.length;i++)
    {
        adj.get(cities[i][0]-1).add(cities[i][1]-1);
        adj.get(cities[i][1]-1).add(cities[i][0]-1);
    }

    if(c_lib<c_road)
    {
        return c_lib*n;
    }
    
    else
    {
        boolean visited[]=new boolean[n];
        for(int i=0;i<n;i++)
        {
            if(visited[i]==false){
                
                cost+= bfsCalc(adj,i,visited,c_road,c_lib);
                System.out.println(cost);
            }
        }
        return cost;
        
    }
}

static long bfsCalc(ArrayList<ArrayList<Integer>> adj,int s,boolean visited[],int c_road,int c_lib)
{
    int cost=0;
    cost=cost+c_lib;
    visited[s]=true;
    Queue<Integer> q=new LinkedList<>();
    q.add(s);
    int node=1;
    while(!q.isEmpty())
    {
        int u=q.poll();
        for(int v : adj.get(u))
        {
            if(visited[v]==false){
            node++;
            visited[v]=true;
            q.add(v);
            }
        }
    }
    
    cost+=(c_road)*(node-1);
    return cost;
}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

    int q = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int qItr = 0; qItr < q; qItr++) {
        String[] nmC_libC_road = scanner.nextLine().split(" ");

        int n = Integer.parseInt(nmC_libC_road[0]);

        int m = Integer.parseInt(nmC_libC_road[1]);

        int c_lib = Integer.parseInt(nmC_libC_road[2]);

        int c_road = Integer.parseInt(nmC_libC_road[3]);

        int[][] cities = new int[m][2];

        for (int i = 0; i < m; i++) {
            String[] citiesRowItems = scanner.nextLine().split(" ");
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

            for (int j = 0; j < 2; j++) {
                int citiesItem = Integer.parseInt(citiesRowItems[j]);
                cities[i][j] = citiesItem;
            }
        }

        long result = roadsAndLibraries(n, c_lib, c_road, cities);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    }

    bufferedWriter.close();

    scanner.close();
}

}

Hi Sarvesh
I will be sharing out the logic of the code, try to implement these changes in your code and then check.

  1. We can’t build new roads in HackerLand, we can only repair existing ones. When repairing roads, we need to be remember that there is no guarantee that each and every city is connected, meaning the graph may be disconnected. For example, a group of 3 cities might be connected to each other, but not connected to 2 other cities that are only connected to one another. We’ll call each of these groups components, and in order to solve the whole problem we’ll need to solve the components individually.

  2. Each component needs at least one library. Without a library in one of the component’s cities, there is no way for the cities in the component to access a library.

  3. There are two ways to assemble a component:

  • A library must exist in at least one city, so s-1 roads must be repaired (where s is the number of cities in the component).
  • A library must exist in every city in a component, meaning that no roads need to be repaired. We choose this option when the cost of building a library is less than the cost of repairing a road.

The minimum cost for each component will either be cost(lib) + (s-1) x cost(road) when we repair roads, or s x cost(lib) when we build a library in each city.
Sum the minimum of each component then to find final solution

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.