-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.cpp
More file actions
207 lines (168 loc) · 4.69 KB
/
Copy pathKruskal.cpp
File metadata and controls
207 lines (168 loc) · 4.69 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <fstream>
#include <vector>
#include <algorithm>
//TODO: Edge struct to store a triplet of {parent, child, cost}
struct Edge {
int parent;
int child;
int cost;
friend bool operator < (const Edge &a, const Edge &b) {
return a.cost < b.cost;
}
};
//TODO: InParser and OutParser classes to handle I/O
class InParser {
private:
std::vector<char> str;
int ptr;
std::ifstream fin;
private:
char get_char() {
if (ptr == (int)str.size()) {
fin.read(str.data(), (int)str.size());
ptr = 0;
}
return str[ptr++];
}
template<class T>
T get_int() {
char chr;
do {
chr = get_char();
} while (!isdigit(chr) && (chr != '-'));
int sgn = +1;
if (chr == '-') {
sgn = -1;
chr = get_char();
}
T num = 0;
while (isdigit(chr)) {
num = num * 10 + (chr - '0');
chr = get_char();
}
return sgn * num;
}
public:
InParser(const char *name) : str((int)1e5), ptr((int)str.size()), fin(name) { }
~InParser() { fin.close(); }
template<class T>
friend InParser &operator >> (InParser &in, T &num) {
num = in.get_int<T>();
return in;
}
};
class OutParser {
private:
std::vector<char> str;
int ptr;
std::ofstream fout;
private:
void put_char(char chr) {
if (ptr == (int)str.size()) {
fout.write(str.data(), (int)str.size());
ptr = 0;
}
str[ptr++] = chr;
}
template<class T>
void put_int(T num) {
if (num < 0) {
put_char('-');
num *= -1;
}
if (num > 9)
put_int(num / 10);
put_char(num % 10 + '0');
}
public:
OutParser(const char *name) : str((int)1e5), ptr(0), fout(name) { }
~OutParser() { fout.write(str.data(), ptr); fout.close(); }
template<class T>
friend OutParser &operator << (OutParser &out, const T num) {
out.put_int(num);
return out;
}
friend OutParser &operator << (OutParser &out, const char chr) {
out.put_char(chr);
return out;
}
friend OutParser &operator << (OutParser &out, const char *str) {
for (int i = 0; str[i]; i++)
out.put_char(str[i]);
return out;
}
};
InParser cin("kruskal.in");
OutParser cout("kruskal.out");
//TODO: DSU class for implementing Kruskal's algo
class DisjointSetUnion {
private:
std::vector<int> parent;
std::vector<int> size;
int n;
public:
DisjointSetUnion(const int _n) {
this->n = _n;
parent.resize(n + 1);
size.resize(n + 1, 1);
for (int i = 1; i <= n; i++)
parent[i] = i;
}
~DisjointSetUnion() { }
int find(int u) {
if (u == parent[u])
return u;
return parent[u] = find(parent[u]);
}
void unite(int u, int v) {
u = find(u);
v = find(v);
if (u == v)
return;
if (size[v] > size[u])
std::swap(u, v);
parent[v] = u;
size[u] += size[v];
}
};
//TODO: MST class to solve the problem
class MinimumSpanningTree {
private:
int n;
public:
MinimumSpanningTree(int _n) {
this->n = _n;
}
~MinimumSpanningTree() { }
int kruskal(std::vector<Edge> &list, std::vector<std::pair<int, int>> &ans) {
std::sort(list.begin(), list.end());
DisjointSetUnion dsu(n);
int res = 0;
for (auto w : list) {
int u = w.parent;
int v = w.child;
int c = w.cost;
if (dsu.find(u) != dsu.find(v)) {
dsu.unite(u, v);
ans.push_back({u, v});
res += c;
}
}
return res;
}
};
int n, m, u, v, c;
std::vector<Edge> list;
std::vector<std::pair<int, int>> ans;
int main() {
cin >> n >> m;
while (m--) {
cin >> u >> v >> c;
list.push_back({u, v, c});
}
MinimumSpanningTree mst(n);
cout << mst.kruskal(list, ans) << '\n';
for (auto p : ans)
cout << p.first << ' ' << p.second << '\n';
return 0;
}