-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhyeongjun.cpp
48 lines (47 loc) · 1.01 KB
/
hyeongjun.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define fastio cin.tie(0)->sync_with_stdio(0)
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int n, m;
int p[100001];
int find(int x) {
if(p[x] < 0) return x;
return p[x] = find(p[x]);
}
void set_union(int x, int y) {
x = find(x);
y = find(y);
if(x != y) {
p[x] = y;
}
return ;
}
int main() {
fastio;
cin >> n >> m;
memset(p, -1, sizeof(p));
vector<tuple<int, int, int>> a(m);
for(auto &[t3, t1, t2] : a) {
cin >> t1 >> t2 >> t3;
}
sort(a.begin(), a.end());
int cnt = 0;
int ans = 0;
for(int i = 0; i < m; i++) {
if(cnt == n - 1) break;
auto [t3, t1, t2] = a[i];
int x = t1, y = t2;
t1 = find(t1);
t2 = find(t2);
if(t1 != t2) {
cout << x << ' ' << y << ' ' << t3 << '\n';
set_union(t1, t2);
ans += t3;
cnt += 1;
}
}
cout << ans << '\n';
return 0;
}