-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1922.cpp
More file actions
68 lines (56 loc) · 1022 Bytes
/
1922.cpp
File metadata and controls
68 lines (56 loc) · 1022 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
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
#include<iostream>
#include<queue>
#include<fstream>
#include<tuple>
#include<algorithm>
using namespace std;
struct P {
int s, e, d = 100000;
};
P arr[100000];
bool cmp(P a, P b) {
return a.d < b.d;
}
int u[1001];
int find(int a) {
if (u[a] < 0)return a;
return u[a] = find(u[a]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)return;
if (u[a] < u[b]) {
u[a] += u[b];
u[b] = a;
}
else {
u[b] += u[a];
u[a] = b;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N; cin >> N;
int M; cin >> M;
for (int i = 0; i < M; i++) {
int a, b, c;
cin >> a >> b >> c;
arr[i] = P{ a,b,c };
}
sort(arr, arr + M, cmp);
fill(u, u + 1001, -1);
int sum = 0;
int count = 0;
for (int i = 0; i < M; i++) {
if (count == N - 1)break;
P curr = arr[i];
if (find(curr.s) == find(curr.e))continue;
merge(curr.s, curr.e);
sum += curr.d;
count++;
}
cout << sum;
}