クラスカル法
全域木は、すべての頂点を閉路なしで連結する木です。最小全域木(MST)は、全域木のうち辺の重みの合計が最小のものです。クラスカル法では、辺を重みの昇順に並べ、両端点が異なる連結成分に属するときだけ辺を採用します。端点がすでに連結されているかどうかの判定と、成分の併合には素集合データ構造(DSU)を使います。
この貪欲な選択は安全です。現在異なる成分を結ぶ辺のうち最も軽い辺は、カットの性質により、あるMSTに含めることができます。この選択を繰り返すと閉路のない森ができ、V - 1 本の辺を採用した時点で全域木、つまりMSTになります。辺の重みが負でも並べ替えやカットの性質は変わりません。負の重みの辺も、異なる成分を結ぶなら採用する必要があります。
辺の両端点は対称に扱うため、入力された頂点をそのまま保持すれば十分です。クラスカル法のために端点を入れ替える必要はありません。重みの比較には、減算によるオーバーフローを避ける Integer.compare を使います。最大 V - 1 本の int の重みを合計すると int の範囲を超えることがあるため、合計値は long に保存します。辺数を E、頂点数を V とすると、ソートを含む時間計算量は O(E log E)、DSU の各操作は償却 O(α(V)) です。空間計算量は O(V + E) です。
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
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
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
FastScanner input = new FastScanner();
int vertexCount = input.nextInt();
int edgeCount = input.nextInt();
List<Edge> edges = new ArrayList<>(edgeCount);
for (int i = 0; i < edgeCount; i++) {
int a = input.nextInt() - 1;
int b = input.nextInt() - 1;
int weight = input.nextInt();
edges.add(new Edge(a, b, weight));
}
edges.sort(Comparator.comparingInt(edge -> edge.weight));
DisjointSet sets = new DisjointSet(vertexCount);
long totalWeight = 0;
int acceptedEdges = 0;
for (Edge edge : edges) {
if (!sets.union(edge.a, edge.b)) {
continue;
}
totalWeight += edge.weight;
acceptedEdges++;
if (acceptedEdges == vertexCount - 1) {
break;
}
}
System.out.println(totalWeight);
}
private static final class Edge {
final int a;
final int b;
final int weight;
Edge(int a, int b, int weight) {
this.a = a;
this.b = b;
this.weight = weight;
}
}
private static final class DisjointSet {
private final int[] parent;
private final int[] size;
DisjointSet(int n) {
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
private int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
boolean union(int a, int b) {
int rootA = find(a);
int rootB = find(b);
if (rootA == rootB) {
return false;
}
if (size[rootA] < size[rootB]) {
int temp = rootA;
rootA = rootB;
rootB = temp;
}
parent[rootB] = rootA;
size[rootA] += size[rootB];
return true;
}
}
private static final class FastScanner {
private final BufferedInputStream input = new BufferedInputStream(System.in);
private final byte[] buffer = new byte[1 << 16];
private int length;
private int position;
private int read() throws IOException {
if (position == length) {
length = input.read(buffer);
position = 0;
if (length == -1) {
return -1;
}
}
return buffer[position++];
}
int nextInt() throws IOException {
int c;
do {
c = read();
} while (c <= ' ' && c != -1);
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
int value = 0;
while (c > ' ') {
value = value * 10 + c - '0';
c = read();
}
return sign * value;
}
}
}