-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskals.java
68 lines (62 loc) · 1.67 KB
/
Kruskals.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
59
60
61
62
63
64
65
66
67
68
import java.io.*;
import java.lang.*;
import java.util.*;
class Edge {
int u, v, w;
public Edge(int u, int v, int w) {
this.u = u;
this.v = v;
this.w = w;
}
}
class comparator implements Comparator<Edge> {
public int compare(Edge a, Edge b) {
return a.w - b.w;
}
}
public class Kruskals {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] parent = new int[n];
for(int i=0;i<n;i++)parent[i]=i;
int[] rank = new int[n];
int m = s.nextInt();
TreeSet<Edge> min_heap = new TreeSet<>(new comparator());
for (int i = 0; i < m; i++) {
int a = s.nextInt();
int b = s.nextInt();
int w = s.nextInt();
min_heap.add(new Edge(a, b, w));
}
int count = 0;
long ans=0;
while (count < n-1) {
Edge e = min_heap.pollFirst();
int x=find(e.u,parent);
int y=find(e.v,parent);
if(x==y)continue;
System.out.println(e.u+" "+e.v+" "+e.w);
ans+=e.w;
Union(parent, rank, x, y);
count++;
}
System.out.println(ans);
}
static void Union(int[] parent,int[] rank,int i,int j){
int x=find(i,parent);
int y=find(j,parent);
if(rank[x]<rank[y]){
parent[x]=y;
}else if(rank[x]>rank[y]){
parent[y]=x;
}else{
parent[x]=y;
rank[y]++;
}
}
static int find(int i,int[] parent){
if(parent[i]==i)return i;
return parent[i]=find(parent[i], parent);
}
}