πŸ“¦1647: λ„μ‹œ λΆ„ν•  κ³„νš


[μ•Œκ³ λ¦¬μ¦˜ 풀이] κ°•λ™κ·œ
Reviewed by Kade Kang (devkade12@gmail.com)
Reviewed:: 2024-02-20
문제 ν™•μΈν•˜κΈ°

풀이

  • MST λ₯Ό κ΅¬ν˜„ν•œ λ¬Έμ œμ΄λ‹€.
  • 2 개둜 λ„μ‹œλ₯Ό λΆ„λ¦¬ν•˜κ³  길의 μœ μ§€λΉ„μ˜ μ΅œμ†Ÿκ°’μ„ κ°–λŠ” 방법은 κ°€μž₯ μœ μ§€λΉ„κ°€ 높은 λ„μ‹œλ₯Ό λ–Όμ–΄λ‚΄μ–΄ λΆ„λ¦¬ν•˜λŠ” 것이닀.
  • μ—°κ²°λ˜μ–΄μžˆμ§€ μ•Šμ€ 경우 μ—°κ²°μ‹œν‚€κ³  μœ μ§€λΉ„μš©μ„ 더해쀀닀.
  • κ°€μž₯ λ§ˆμ§€λ§‰ λ”ν•΄μ£ΌλŠ” 값을 μ œμ™Έν•œλ‹€.
#include <iostream>
#include <vector>
#include <algorithm>
#define endl '\n'
using namespace std;
 
int N, M, res=0, last;
int parent[100001];
vector<pair<int, pair<int, int>>> edge;
 
void Input(){
    cin >> N >> M;
    for(int i=1; i<=N; i++){
        parent[i]=i;
    }
    int a, b, c;
    for(int i=0; i<M; i++){
        cin >> a >> b >> c;
        edge.push_back({c,{a, b}});
    }
    sort(edge.begin(), edge.end());
}
 
int Find(int x){
    if (parent[x] == x) return x;
    else return parent[x] = Find(parent[x]);
}
 
void Union(int x, int y){
    x = Find(x);
    y = Find(y);
    if(x != y){
        parent[y] = x;
    }
}
 
int sameParent(int x, int y){
    x = Find(x);
    y = Find(y);
    if(x == y) return 1;
    return 0;
}
 
void Solution(){
    for(int i=0; i<edge.size(); i++){
        if(sameParent(edge[i].second.first, edge[i].second.second) == 0){
            Union(edge[i].second.first, edge[i].second.second);
            last = edge[i].first;
            res += last;
        }
    }
    cout << res - last << endl;
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
 
    Input();
    Solution();
 
    return 0;
}