Skip to content

Commit

Permalink
33.py
Browse files Browse the repository at this point in the history
  • Loading branch information
huisuu committed Aug 22, 2024
1 parent 68c8d07 commit 6cdfa10
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions HSKIM/31to40/33.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def find(parents, x):
if parents[x] == x:
return x
parents[x] = find(parents, parents[x])
return parents[x]

def union(parents, x, y):
root1 = find(parents, x)
root2 = find(parents, y)

parents[root2] = root1

def solution(k, operation):
parents = list(range(k))
n = k

for op in operation:
if op[0] == "u":
union(parents, op[1], op[2])
elif op[0] == "f":
find(parents, op[1])

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

return n

0 comments on commit 6cdfa10

Please sign in to comment.