[PS] 최소 신장 트리(MST) 구현(C++)

2024. 8. 31. 23:44·PS

<!-- 작성중인 게시글입니다. -->

 

#include <bits/stdc++.h>
using namespace std;

int parent[10001];
vector<pair<int, pair<int, int>>> edges;

// find
int find(int x) {
    if (x == parent[x]) return x;
    else return parent[x] = find(parent[x]);
}
// union
void merge(int a, int b) {
    int x = find(a);
    int y = find(b);
    if (x == y) return;
    parent[y] = x; // y -> x
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int v, e;
    long long result = 0;
    cin >> v >> e;
    while (e--) {
        int st, ed, weight;
        cin >> st >> ed >> weight;
        edges.push_back({weight, {st, ed}});
    }
    sort(edges.begin(), edges.end());
    for (int i = 1; i <= v; i++) parent[i] = i;
    for (auto edge : edges) {
        if (find(edge.second.first) != find(edge.second.second)) {
            merge(edge.second.first, edge.second.second);
            result += edge.first;
        }
    }
    cout << result;
}

 

관련 문제

https://www.acmicpc.net/problem/1197

'PS' 카테고리의 다른 글

[PS] 비트 마스킹의 응용 - 모든 부분집합, 비트DP  (0) 2024.09.04
[PS] Union-Find(유니온-파인드)  (0) 2024.08.30
'PS' 카테고리의 다른 글
  • [PS] 비트 마스킹의 응용 - 모든 부분집합, 비트DP
  • [PS] Union-Find(유니온-파인드)
lumana
lumana
배움을 나누는 공간 https://github.com/bebeis
  • lumana
    Brute force Study
    lumana
  • 전체
    오늘
    어제
    • 분류 전체보기 (456)
      • Software Development (27)
        • Performance (0)
        • TroubleShooting (1)
        • Refactoring (0)
        • Test (8)
        • Code Style, Convetion (0)
        • DDD (0)
        • Software Engineering (18)
      • Java (71)
        • Basic (5)
        • Core (21)
        • Collection (7)
        • 멀티스레드&동시성 (13)
        • IO, Network (8)
        • Reflection, Annotation (3)
        • Modern Java(8~) (12)
        • JVM (2)
      • Spring (53)
        • Framework (12)
        • MVC (23)
        • Transaction (3)
        • AOP (11)
        • Boot (0)
        • AI (0)
      • DB Access (1)
        • Jdbc (1)
        • JdbcTemplate (0)
        • JPA (14)
        • Spring Data JPA (0)
        • QueryDSL (0)
      • Computer Science (129)
        • Data Structure (27)
        • OS (14)
        • Database (10)
        • Network (21)
        • 컴퓨터구조 (5)
        • 시스템 프로그래밍 (23)
        • Algorithm (29)
      • HTTP (8)
      • Infra (1)
        • Docker (1)
      • 프로그래밍언어론 (15)
      • Programming Language(Sub) (77)
        • Kotlin (1)
        • Python (25)
        • C++ (51)
        • JavaScript (0)
      • FE (11)
        • HTML (1)
        • CSS (9)
        • React (0)
        • Application (1)
      • Unix_Linux (0)
        • Common (0)
      • PS (13)
        • BOJ (7)
        • Tip (3)
        • 프로그래머스 (0)
        • CodeForce (0)
      • Book Review (4)
        • Clean Code (4)
      • Math (3)
        • Linear Algebra (3)
      • AI (7)
        • DL (0)
        • ML (0)
        • DA (0)
        • Concepts (7)
      • 프리코스 (4)
      • Project Review (6)
      • LegacyPosts (11)
      • 모니터 (0)
      • Diary (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
lumana
[PS] 최소 신장 트리(MST) 구현(C++)
상단으로

티스토리툴바