forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
58 lines (55 loc) · 1.84 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static long solve(int n, long cl, long cr, ArrayList<TreeSet<Integer>> adj) {
if (cl <= cr) { return (cl * n); }
long cost = 0;
boolean[] visited = new boolean[n];
for (int i = 0; i < n; i++) {
if (!visited[i]) {
long tmpCost = cl;
Stack<Integer> stack = new Stack<>();
stack.push(i);
visited[i] = true;
while (!stack.empty()) {
int u = stack.pop();
Iterator<Integer> it = adj.get(u).iterator();
while (it.hasNext()) {
int v = it.next();
if (!visited[v]) {
stack.push(v);
visited[v] = true;
tmpCost += cr;
}
}
}
cost += tmpCost;
}
}
return cost;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++){
int n = in.nextInt();
int m = in.nextInt();
long cl = in.nextLong();
long cr = in.nextLong();
ArrayList<TreeSet<Integer>> adj = new ArrayList<>();
for (int i = 0; i < n; i++) { adj.add(new TreeSet<Integer>()); }
for(int a1 = 0; a1 < m; a1++){
int u = in.nextInt();
u--;
int v = in.nextInt();
v--;
adj.get(u).add(v);
adj.get(v).add(u);
}
System.out.println(Solution.solve(n, cl, cr, adj));
}
}
}