forked from encrypted-def/basic-algo-lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1197.cpp
48 lines (42 loc) · 909 Bytes
/
1197.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
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/aaafcfd71b164a75a8b89343bef17a46
#include <bits/stdc++.h>
using namespace std;
vector<int> p(10005,-1);
int find(int x){
if(p[x] < 0) return x;
return p[x] = find(p[x]);
}
bool is_diff_group(int u, int v){
u = find(u); v = find(v);
if(u == v) return 0;
if(p[u] == p[v]) p[u]--;
if(p[u] < p[v]) p[v] = u;
else p[u] = v;
return 1;
}
int v, e;
tuple<int,int,int> edge[100005];
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> v >> e;
for(int i = 0; i < e; i++){
int a, b, cost;
cin >> a >> b >> cost;
edge[i] = {cost, a, b};
}
sort(edge, edge + e);
int cnt = 0;
int ans = 0;
for(int i = 0; i < e; i++){
int a, b, cost;
tie(cost, a, b) = edge[i];
if(!is_diff_group(a, b)) continue;
ans += cost;
cnt++;
if(cnt == v-1) break;
}
cout << ans;
}