-
Notifications
You must be signed in to change notification settings - Fork 1
/
sol.java
55 lines (46 loc) · 1.59 KB
/
sol.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
// Notice the use of longs in this solution. It's because the final cost is too large
// for 32-bit integers in some inputs!
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
private static ArrayList<Integer>[] g;
private static boolean[] visited;
private static long roads, libs;
private static void dfs(Integer node) {
visited[node] = true;
for (Integer next : g[node])
if (!visited[next]) {
roads++;
dfs(next);
}
}
public static void main(String[] args) throws IOException {
int q = scanner.nextInt();
while (q-- > 0) {
int n = scanner.nextInt(); int m = scanner.nextInt();
long clib = scanner.nextLong(); long croad = scanner.nextLong();
roads = 0; libs = 0;
visited = new boolean[n];
g = new ArrayList[n];
for (int i = 0; i < n; i++) g[i] = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
Integer u = scanner.nextInt(); Integer v = scanner.nextInt();
u--; v--;
g[u].add(v); g[v].add(u);
}
if (clib <= croad) {
System.out.println(n * clib);
continue;
}
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
dfs(i);
libs++;
}
System.out.println(croad*roads + clib*libs);
}
}
}