Skip to content

Commit

Permalink
Merge pull request #73 from AlgoLeadMe/19-seongwon030
Browse files Browse the repository at this point in the history
19-seongwon030
  • Loading branch information
seongwon030 authored Jul 22, 2024
2 parents 645a3da + 051e59c commit 8b757c8
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
input = sys.stdin.readline

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


while True:
V,E = map(int,input().split())
if V==0 and E==0:
break
root = [i for i in range(V+1)]
edge = [] # ๊ฐ„์„ ๋ฆฌ์ŠคํŠธ
for i in range(E):
a,b,c = map(int,input().split())
edge.append((a,b,c))

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

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

print(ans)

0 comments on commit 8b757c8

Please sign in to comment.