From 7aae2442d27975e44c70e3e8f9b195ae70dfda4c Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Sat, 8 Nov 2025 19:59:09 +0900 Subject: [PATCH] 20251108 baekjoon --- code_study/Baekjoon/python/1197.py | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 code_study/Baekjoon/python/1197.py diff --git a/code_study/Baekjoon/python/1197.py b/code_study/Baekjoon/python/1197.py new file mode 100644 index 0000000..a31ed07 --- /dev/null +++ b/code_study/Baekjoon/python/1197.py @@ -0,0 +1,35 @@ +import sys +sys.setrecursionlimit(10**6) + +V, E = map(int, input().split()) + +edges = [] +for _ in range(E) : + u, v, w = map(int, input().split()) + edges.append((w,u,v)) + +edges.sort() + +parent = [i for i in range(V+1)] +total_weight = 0 + +def find_parent(x) : + if x != parent[x] : + parent[x] = find_parent(parent[x]) + return parent[x] + +def union_parent(x, y) : + parentX = find_parent(x) + parentY = find_parent(y) + + if parentX < parentY : + parent[parentY] = parentX + else : + parent[parentX] = parentY + +for w, u, v in edges : + if find_parent(u) != find_parent(v) : + total_weight += w + union_parent(u, v) + +print(total_weight) \ No newline at end of file