From 0912033956e713484f530b589c607ae7808ef581 Mon Sep 17 00:00:00 2001 From: emost22 Date: Fri, 20 Oct 2023 20:26:08 +0900 Subject: [PATCH] =?UTF-8?q?boj=206497=20=EC=A0=84=EB=A0=A5=EB=82=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Kruskal/6497.cpp" | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 "\354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254/Kruskal/6497.cpp" diff --git "a/\354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254/Kruskal/6497.cpp" "b/\354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254/Kruskal/6497.cpp" new file mode 100644 index 00000000..792cde56 --- /dev/null +++ "b/\354\265\234\354\206\214 \354\212\244\355\214\250\353\213\235 \355\212\270\353\246\254/Kruskal/6497.cpp" @@ -0,0 +1,85 @@ +#include +#include +#include +#define MAX 200001 +using namespace std; + +typedef struct Node { + int u, v, w; +}Node; + +Node list[MAX]; +int parent[MAX]; +int N, M, sum; + +bool cmp(Node a, Node b) { + return a.w < b.w; +} + +void init() { + for (int i = 0; i < N; i++) { + parent[i] = i; + } +} + +int find(int v) { + if (parent[v] == v) return v; + return parent[v] = find(parent[v]); +} + +bool unionNode(int u, int v) { + u = find(u); + v = find(v); + + if (parent[u] != parent[v]) { + parent[v] = parent[u]; + return true; + } + + return false; +} + +void func() { + init(); + + sort(list, list + M, cmp); + + int ret = 0; + int cnt = 0; + for (int i = 0; i < M; i++) { + int u = list[i].u; + int v = list[i].v; + + if (unionNode(u, v)) { + ret += list[i].w; + cnt++; + } + + if (cnt == N - 1) break; + } + + cout << sum - ret << '\n'; +} + +void input() { + cin >> N >> M; + if (!N && !M) exit(0); + + sum = 0; + for (int i = 0; i < M; i++) { + cin >> list[i].u >> list[i].v >> list[i].w; + sum += list[i].w; + } +} + +int main() { + cin.tie(NULL); cout.tie(NULL); + ios::sync_with_stdio(false); + + while (1) { + input(); + func(); + } + + return 0; +} \ No newline at end of file