diff --git a/HSKIM/31to40/33.py b/HSKIM/31to40/33.py new file mode 100644 index 0000000..b26a7c5 --- /dev/null +++ b/HSKIM/31to40/33.py @@ -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 \ No newline at end of file