Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/tiaz0128/ch_10/solution_033.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
from typing import List


def find(parents: List[int], target: int):
# 2. 부모 노드가 루트 노드인지 확인 (index == value)
if parents[target] == target:
return target

# 3. 아닌 경우 계속해서 부모 노드를 타고 올라간다.
parents[target] = find(parents, parents[target])
return parents[target]


def union(parents: List[int], x, y):
# 1. 각각 루트 노드를 찾는다.
root_x = find(parents, x)
root_y = find(parents, y)

# 2. 찾은 루트 노드의 크기를 비교
parents[root_y] = root_x


def solution(k, operations):
return
parents = list(range(k))

for operator, *nodes in operations:
if operator == "u":
union(parents, *nodes)
elif operator == "f":
find(parents, *nodes)

return len(set(find(parents, i) for i in range(k)))