-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1197.cpp
More file actions
37 lines (34 loc) · 758 Bytes
/
1197.cpp
File metadata and controls
37 lines (34 loc) · 758 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
#include <bits/stdc++.h>
using namespace std;
int v, e, ans, cnt;
tuple<int,int,int> edge[100001];
vector<int> p(10001, -1);
int find(int cur) {
if(p[cur] == -1) return cur;
return p[cur] = find(p[cur]);;
}
bool isGroup(int a, int b) {
a = find(a); b = find(b);
if(a == b) return 1;
if(a < b) p[a] = b;
else p[a] = b;
return 0;
}
int main() {
cin >> v >> e;
for(int i = 0; i < e; i++) {
int a, b, c; cin >> a >> b >> c;
edge[i] = {c, a, b};
}
sort(edge, edge + e);
for(int i = 0; i < e; i++){
int cost, a, b;
tie(cost, a, b) = edge[i];
if(!isGroup(a, b)) {
ans += cost;
cnt++;
}
if(cnt == v - 1) break;
}
cout << ans;
}