Skip to content

Commit

Permalink
Merge pull request #67 from AlgoLeadMe/17-seongwon030
Browse files Browse the repository at this point in the history
17-seongwon030
  • Loading branch information
seongwon030 authored Jul 22, 2024
2 parents d280707 + 0d3af7f commit 645a3da
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys

input = sys.stdin.readline

V,E = map(int,input().split())
root = [i for i in range(V+1)]
edge = [] # ๊ฐ„์„ ๋ฆฌ์ŠคํŠธ
for i in range(E):
edge.append(list(map(int,input().split())))

# ๋น„์šฉ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ
edge.sort(key=lambda x:x[2])

def find(x):
if x!=root[x]:
root[x] = find(root[x])
return root[x]

ans = 0
for a,b,c in edge:
aRoot = find(a)
bRoot = find(b)
if aRoot != bRoot:
if aRoot > bRoot:
root[aRoot] = bRoot
else:
root[bRoot] = aRoot
ans += c

print(ans)

0 comments on commit 645a3da

Please sign in to comment.