-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
185 lines (138 loc) · 4.18 KB
/
Solution.java
File metadata and controls
185 lines (138 loc) · 4.18 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package hackrank.algorithm.graph.kruskalmst;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
* Kruskal (MST): Really Special Subtree
*
* @see https://www.hackerrank.com/challenges/kruskalmstrsub
*/
public class Solution {
public static void main(String[] args) {
Graph graph = readInput(System.in);
System.out.println(graph.findKruskalMinSpanWeight());
}
private static Graph readInput(InputStream input) {
Scanner scanner = new Scanner(input);
int numberNodes = scanner.nextInt();
int numberEdges = scanner.nextInt();
int[][] edges = new int[numberEdges][];
for (int e = 0; e < numberEdges; e++) {
edges[e] = new int[] { scanner.nextInt(), scanner.nextInt(), scanner.nextInt() };
}
scanner.close();
return new Graph(numberNodes, edges);
}
}
class Graph {
public Map<Integer, Node> nodes;
public List<Edge> edges;
public Graph(int numberNodes, int[][] edges) {
this.nodes = new HashMap<>(numberNodes, 1.0f);
this.edges = new ArrayList<>(edges.length);
for (int nodeId = 1; nodeId <= numberNodes; nodeId++) {
nodes.put(nodeId, new Node(nodeId));
}
for (int[] edge : edges) {
connect(edge[0], edge[1], edge[2]);
}
}
public void connect(int idFrom, int idTo, int weight) {
Node from = nodes.get(idFrom);
Node to = nodes.get(idTo);
edges.add(new Edge(from, to, weight));
}
public int findKruskalMinSpanWeight() {
Collections.sort(edges);
int totalEdgeWeight = 0;
UnionFind unionFind = new UnionFind();
for (Edge edge : edges) {
if (!unionFind.isConnected(edge.from, edge.to)) {
totalEdgeWeight += edge.weight;
unionFind.union(edge.from, edge.to);
}
}
return totalEdgeWeight;
}
}
class Node {
public int id;
public Node unionLead;
public Node(int id) {
this.id = id;
}
@Override
public String toString() {
return String.valueOf(id);
}
}
class Edge implements Comparable<Edge> {
public Node from;
public Node to;
public int weight;
public Edge(Node from, Node to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
@Override
public int compareTo(Edge other) {
int diff = Integer.compare(weight, other.weight);
if (diff == 0) {
return Integer.compare(from.id + to.id, other.from.id + other.to.id);
} else {
return diff;
}
}
@Override
public String toString() {
return "N(" + to.id + ") --" + weight + "-- N(" + from.id + ")";
}
}
class UnionFind {
private Map<Integer, List<Node>> leads;
public UnionFind() {
this.leads = new HashMap<>();
}
public boolean isConnected(Node from, Node to) {
return from.unionLead != null && from.unionLead == to.unionLead;
}
public void union(Node from, Node to) {
if (isConnected(from, to)) {
return;
}
if (from.unionLead == null) {
initLead(from);
}
if (to.unionLead == null) {
initLead(to);
}
if (getSetSize(from) >= getSetSize(to)) {
setNewLead(from.unionLead, to);
} else {
setNewLead(to.unionLead, from);
}
}
private void initLead(Node node) {
node.unionLead = node;
List<Node> nodes = new ArrayList<>();
nodes.add(node);
leads.put(node.id, nodes);
}
private int getSetSize(Node node) {
return leads.get(node.unionLead.id).size();
}
private void setNewLead(Node lead, Node follower) {
Node prevLead = follower.unionLead;
List<Node> prevFollowers = leads.get(prevLead.id);
leads.remove(prevLead.id);
for (Node prevFollower : prevFollowers) {
prevFollower.unionLead = lead;
leads.get(lead.id).add(prevFollower);
}
}
}