-
Notifications
You must be signed in to change notification settings - Fork 1
/
kruskal.h
100 lines (85 loc) · 2.4 KB
/
kruskal.h
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
class UnionFind {
private:
vector<int> parent; // 親
vector<int> rank; // 木の深さ
public:
explicit UnionFind(int n); // n要素で初期化
int Find(int x); // 木の根を返す
void Unite(int x, int y); // xとyの属する集合を併合
bool Same(int x, int y); // xとyが同じ集合に属するか否か
};
UnionFind::UnionFind(int n) : parent(vector<int>(n)), rank(vector<int>(n))
{
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
int UnionFind::Find(int x) {
if (parent[x] == x) {
return x;
} else {
return parent[x] = Find(parent[x]);
}
}
void UnionFind::Unite(int x, int y) {
x = Find(x);
y = Find(y);
if (x == y) return;
if (rank[x] < rank[y]) {
parent[x] = y;
} else {
parent[y] = x;
if (rank[x] == rank[y]) rank[x]++;
}
}
bool UnionFind::Same(int x, int y) { return Find(x) == Find(y); }
template <typename T>
struct Edge {
int src, to;
T cost;
Edge(int to, T cost) : src(-1), to(to), cost(cost) {}
Edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
};
template <typename T>
using Edges = vector<Edge<T>>;
template <typename T>
using AdjList = vector<Edges<T>>;
template <typename T>
class Kruskal {
private:
int V; // 頂点数
Edges<T> edges;
AdjList<T> minimumSpanningTree;
public:
explicit Kruskal(int n);
void AddEdge(int from, int to, T cost);
T Run();
AdjList<T> GetMinimumSpanningTree();
};
template <typename T>
Kruskal<T>::Kruskal(int n) : V(n), minimumSpanningTree(n) {}
template <typename T>
void Kruskal<T>::AddEdge(int from, int to, T cost) {
edges.push_back(Edge<T>(from, to, cost));
}
template <typename T>
T Kruskal<T>::Run() {
sort(edges.begin(), edges.end(),
[](const Edge<T> &a, const Edge<T> &b) { return a.cost < b.cost; });
UnionFind tree(V);
T total = 0;
for (auto &e : edges) {
if (!tree.Same(e.src, e.to)) {
tree.Unite(e.src, e.to);
total += e.cost;
minimumSpanningTree[e.src].push_back(Edge<T>(e.to, e.cost));
minimumSpanningTree[e.to].push_back(Edge<T>(e.src, e.cost));
}
}
return total;
}
template <typename T>
AdjList<T> Kruskal<T>::GetMinimumSpanningTree() {
return minimumSpanningTree;
}