-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKrushKalTLE.java
More file actions
66 lines (59 loc) · 1.7 KB
/
KrushKalTLE.java
File metadata and controls
66 lines (59 loc) · 1.7 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
66
import java.util.*;
class Edge{
int v1,v2,w;
Edge(int v1,int v2,int w){
this.v1 = v1;
this.v2 = v2;
this.w = w;
}
@Override
public String toString(){
return v1 < v2 ? v1+" "+v2+" "+w : v2+" "+v1+" "+w;
}
}
class A implements Comparator<Edge>{
public int compare(Edge o1,Edge o2){
return o1.w - o2.w;
}
}
public class KrushKalTLE {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int V = sc.nextInt();
int E = sc.nextInt();
Edge edges[] = new Edge[E];
Set<Integer> e[] = new HashSet[V];
for(int i=0;i<V;i++) e[i] = new HashSet<>();
for(int i=0;i<E;i++){
edges[i] = new Edge(sc.nextInt(), sc.nextInt(),sc.nextInt());
}
Arrays.sort(edges,new A());
Edge arr[] = MST(edges,V,e);
for(Edge x:arr)
System.out.println(x);
}
static Edge[] MST(Edge edges[],int V,Set<Integer> e[]){
int count = 0, i = 0;
Edge[] ans = new Edge[V-1];
while(count != V-1){
Edge temp = edges[i++];
if(e[temp.v1].contains(temp.v2) || e[temp.v2].contains(temp.v1))
continue;
e[temp.v1].add(temp.v2);
e[temp.v1].addAll(e[temp.v2]);
e[temp.v2].add(temp.v1);
e[temp.v2].addAll(e[temp.v1]);
for(Integer x:e[temp.v1]) {
e[x].add(temp.v2);
e[x].addAll(e[temp.v2]);
}
for(Integer x:e[temp.v2])
{
e[x].add(temp.v1);
e[x].addAll(e[temp.v1]);
}
ans[count++] = temp;
}
return ans;
}
}