-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1197.java
More file actions
65 lines (52 loc) · 1.76 KB
/
Copy pathJ1197.java
File metadata and controls
65 lines (52 loc) · 1.76 KB
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
59
60
61
62
63
64
65
package TIL;
import java.io.*;
import java.util.*;
public class J1197 {
static int[] array;
static ArrayList<int[]>[] list;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
int node = Integer.parseInt(input[0]);
int edge = Integer.parseInt(input[1]);
array = new int[node+1];
list = new ArrayList[edge];
for(int i = 0; i<= node; i++){
array[i] = i;
}
int cnt = 0;
long value = 0;
for(int i = 0; i< edge; i++){
list[i] = new ArrayList<>();
input = buffer.readLine().split(" ");
int start = Integer.parseInt(input[0]);
int end = Integer.parseInt(input[1]);
int weight = Integer.parseInt(input[2]);
list[i].add(new int[]{start, end, weight});
}
Arrays.sort(list, Comparator.comparingInt(o -> o.get(0)[2]));
for(int i = 0; i< edge; i++){
int[] now = list[i].get(0);
int min= Math.min(now[0],now[1]);
int max= Math.max(now[0],now[1]);
int x = find(min);
int y = find(max);
if (x != y) {
union(x, y);
cnt++;
value += now[2];
}else continue;
if (cnt == node - 1) break;
}
System.out.println(value);
}
public static void union(int a, int b){
a = find(a);
b = find(b);
if(array[a] != array[b]) array[array[b]] = array[array[a]];
}
public static int find(int a){
if(array[a] == a) return a;
return array[a] = find(array[array[a]]);
}
}