forked from ndb796/Python-Competitive-Programming-Team-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskal.py
More file actions
61 lines (50 loc) · 1.21 KB
/
Copy pathkruskal.py
File metadata and controls
61 lines (50 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
''' [ Kruskal Algorithm]
- 대표적인 최소 신장 트리 알고리즘
- 그래프 내의 모든 정점들을 최소 비용으로 연결하기 위해 사용
- 사이클 발생여부 확인을 위해 서로소 집합 자료구조를 활용한다.
1. 간선을 비용에 따라 오름차순 정렬
2. 간선을 하나씩 확인하며,
사이클 발생O => 최소신장트리에 포함X
사이클 발생X => 최소신장트리에 포함O
'''
def find_parent(parent, x):
if parent[x] != x:
parent[x] = find_parent(parent, parent[x])
return parent[x]
def union_parent(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else: parent[a] = b
v, e = map(int, input().split())
parent = [0] * (v+1)
edges = []
result = 0
for i in range(1, v+1): parent[i] = i
for _ in range(e):
a, b, w = map(int, input().split())
edges.append((w, a, b))
edges.sort()
for w, a, b in edges:
if find_parent(parent, a) == find_parent(parent, b):
continue
else:
union_parent(parent, a, b)
result += w
print(result)
'''
[Input Example 1]
7 9
1 2 29
1 5 75
2 3 35
2 6 34
3 4 7
4 6 23
4 7 13
5 6 53
6 7 25
[Output Example 1]
159
'''