20251108 baekjoon
This commit is contained in:
parent
fed6066fbf
commit
7aae2442d2
35
code_study/Baekjoon/python/1197.py
Normal file
35
code_study/Baekjoon/python/1197.py
Normal file
@ -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)
|
||||
Loading…
x
Reference in New Issue
Block a user