-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj1197.cpp
More file actions
43 lines (31 loc) · 746 Bytes
/
boj1197.cpp
File metadata and controls
43 lines (31 loc) · 746 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class list {
public:
int x, y, cost;
bool operator()(list i, list j) { return i.cost < j.cost; }
};
int disjoint(int x, vector<int> &uf) {
return (x != uf[x]) ? uf[x] = disjoint(uf[x], uf) : x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector <int> uf(n + 1);
vector <list> v(m);
for (int i = 1; i <= n; i++) uf[i] = i;
for (int i = 0; i < m; i++)
cin >> v[i].x >> v[i].y >> v[i].cost;
sort(v.begin(), v.end(), list());
int ans = 0;
for (int i = 0; i < m; i++)
if (disjoint(v[i].y, uf) != disjoint(v[i].x, uf)) {
uf[disjoint(v[i].y, uf)] = uf[v[i].x];
ans += v[i].cost;
}
cout << ans;
}