This is Discussion thread about Religious People
Discussion About Religious People
import java.util.*;
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t–>0) {
int n=sc.nextInt();
int m=sc.nextInt();
long a=sc.nextInt();
long b=sc.nextInt();
HashMap<Integer,HashMap<Integer,Long>> hm=new HashMap<>();
for(int i=1;i<=n;i++){
hm.put(i,new HashMap<>());
}
for(int i=0;i<m;i++){
int u=sc.nextInt();
int v= sc.nextInt();
hm.get(u).put(v,b);
hm.get(v).put(u,b);
}
ArrayList al=bfs(hm,b);
long hehe=0;
for (int i = 0; i <al.size() ; i++) {
hehe+=al.get(i);
}
long k=(al.size()a)+hehe;
long z=na;
if(k<z) System.out.println(k);
else System.out.println(z);
}
}
public static ArrayList bfs(HashMap<Integer,HashMap<Integer,Long>> hm,Long b){
ArrayList al=new ArrayList<>();
HashSet visited=new HashSet<>();
Queue q=new LinkedList<>();
for(int src:hm.keySet()) {
long cost=0;
if(visited.contains(src)){
continue;
}
q.add(src);
while (!q.isEmpty()) {
int rt = q.remove();
if (visited.contains(rt)) {
continue;
} else visited.add(rt);
for (int nbrs : hm.get(rt).keySet()) {
if (!visited.contains(nbrs)) {
cost+=b;
q.add(nbrs);
visited.add(nbrs);
}
}
}
al.add(cost);
}
return al;
}
}
why it is giving error