/
1922.cpp
54 lines (53 loc) · 1.53 KB
/
1922.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
49
50
51
52
53
54
#include <stdio.h>
#include <tuple>
#include <algorithm>
using namespace std;
tuple<int, int, int> edge[100001]; // cost, com1, com2
int parent[1001]; // union-find에 쓰일 예정
int find(int i) { // find a root of i
int root = i;
while (parent[root] >= 0) {
root = parent[root];
}
int child = i;
while (child != root) {
int tmp = parent[child];
parent[child] = root;
child = tmp;
}
return root;
}
void uni(int i, int j) {
int tmp = parent[i] + parent[j];
if (parent[i] > parent[j]) { // i가 가지고 있는 자식이 j가 가지고 있는 자식보다 적을 경우
parent[i] = j; // j가 i의 root가 됨
parent[j] = tmp; // j가 거느리는 자식이 많아짐
}
else {
parent[j] = i; // i가 j의 root가 됨
parent[i] = tmp; // j가 거느리는 자식이 많아짐
}
}
int main(void) {
int N, M;
scanf("%d %d", &N, &M);
for (int i = 1; i <= N; i++)
parent[i] = -1;
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &get<1>(edge[i]), &get<2>(edge[i]), &get<0>(edge[i]));
}
sort(edge, edge + M);
int cnt = 0;
int cost = 0;
int idx = 0;
while (cnt < N - 1) {
if (find(get<1>(edge[idx])) == find(get<2>(edge[idx]))) { // com1과 com2가 이미 묶여있으면
idx++; continue;
}
//묶여있지 않으면
uni(find(get<1>(edge[idx])), find(get<2>(edge[idx])));
cost += get<0>(edge[idx]);
cnt++;
}
printf("%d", cost);
}