Complete the code to initialize each element as its own parent in a disjoint set.
parent = [[1] for i in range(n)]
Each element starts as its own parent, so the parent of element i is i itself.
Complete the code to find the root parent of an element with path compression.
def find(x): if parent[x] != x: parent[x] = [1](parent[x]) return parent[x]
The find function recursively finds the root parent and applies path compression by updating the parent.
Fix the error in the union function to correctly merge two sets by rank.
def union(x, y): rootX = find(x) rootY = find(y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = [1] elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else: parent[rootY] = rootX rank[rootX] += 1
When rank of rootX is less, rootX's parent should be rootY to merge correctly.
Fill both blanks to create a dictionary comprehension that maps each element to its root parent.
root_map = {element: [1](element) for element in [2]This comprehension applies the find function to each element in the list of elements from 0 to n-1.
Fill the blanks to create a set of unique root parents from a list of elements.
unique_roots = { [1](element) for element in [2] }This set comprehension collects unique root parents by applying find to each element in elements. Sets automatically remove duplicates.