Complete the code to initialize the parent array for union-find.
parent = [[1] for i in range(n)]
Each node is initially its own parent, so we assign parent[i] = i.
Complete the code to find the root parent of a node with path compression.
def find(x): if parent[x] != x: parent[x] = [1](parent[x]) return parent[x]
The find function calls itself recursively to find the root parent and compress the path.
Fix the error in the union function to correctly merge two sets.
def union(a, b): rootA = find(a) rootB = find(b) if rootA != rootB: parent[[1]] = rootB
We assign the parent of rootA to rootB to merge sets.
Fill in the blank to sort edges by weight.
edges = sorted(edges, key=lambda x: x[1])
Edges are tuples (u, v, w); the weight is at index 2, accessed by x[2].
Fill all three blanks to add edge weight to result if union is successful.
result = 0 for u, v, w in edges: if find(u) != find(v): union([1], [2]) result [3]= w
We union the two nodes u and v, and add the weight w to the result.
