-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1197.cpp
53 lines (43 loc) · 1.08 KB
/
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
49
50
51
52
53
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, pair<int, int>> T;
int V, E;
vector<T> v;
int parent[10000+1];
int ans;
int findParent(int x){
if(parent[x] == x) return x;
return parent[x] = findParent(parent[x]);
}
void unionParent(int a, int b){
a = findParent(a);
b = findParent(b);
parent[b] = a;
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> V >> E;
for(int i = 0; i < E; i++){
int a, b, c;
cin >> a >> b >> c;
v.push_back({c, {a, b}});
}
sort(v.begin(), v.end());
for(int i = 1; i <= V; i++)
parent[i] = i;
int cnt = 0;
for(int i = 0; i < v.size(); i++){
T curEdge = v[i];
int cost = curEdge.first;
int now = curEdge.second.first;
int next = curEdge.second.second;
if(findParent(now) == findParent(next)) continue; // 사이클이면 pass
unionParent(now, next);
ans += cost;
if(++cnt == V - 1) break;
}
cout << ans << '\n';
return 0;
}