링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42861
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int parent[101];
int getParent(int x) {
if(parent[x] == x) return x;
return parent[x] = getParent(parent[x]);
}
bool cmp(vector<int> a, vector<int> b) {
return a[2] < b[2];
}
int solution(int n, vector<vector<int>> costs) {
int answer = 0;
for(int i = 0; i < n; i++)
parent[i] = i;
sort(costs.begin(), costs.end(), cmp);
for(int i = 0; i < costs.size(); i++) {
int start = getParent(costs[i][0]);
int end = getParent(costs[i][1]);
int cost = costs[i][2];
if(start != end) {
answer += cost;
parent[end] = start;
}
}
return answer;
}