-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjoint.py
More file actions
33 lines (28 loc) · 999 Bytes
/
Disjoint.py
File metadata and controls
33 lines (28 loc) · 999 Bytes
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
def find_parent(parent, x):
# if parent[x]!=x: # x 부모가 x가 아닐 때(root가 아닐 때)
# return find_parent(parent, parent[x]) # root로 거슬러 올라감
# return x
if parent[x]!=x:
parent[x] = find_parent(parent, parent[x])
# root까지 갱신
return parent[x]
# root까지의 거리가 짧아지므로 검색 시간이 빨라진다.
def union_parent(parent, a,b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b: # a가 부모
parent[b] = a
else:
parent[a] = b
v, e = map(int, input().split()) # vertex, edge(union) 개수 입력
parent = [i for i in range(v+1)]
for i in range(e):
a,b = map(int, input().split())
union_parent(parent, a,b)
print('각 원소가 속한 집합: ',end='')
for i in range(1, v+1):
print(find_parent(parent, i),end=' ')
print()
print('부모 테이블: ',end='')
for i in range(1, v+1):
print(parent[i], end=' ')