Question DSA-GRAPH-0017
HardWhat is the output of this code?
edges = sorted([(1, 0, 1), (2, 1, 2), (3, 2, 3), (4, 0, 2), (5, 1, 3)])
parent = list(range(4))
def find(x):
while parent[x] != x:
x = parent[x]
return x
total = 0
for w, a, b in edges:
ra, rb = find(a), find(b)
if ra != rb:
parent[ra] = rb
total += w
print(total)
- A.
7
- B.
5
- C.
10
- D.
6
Sign up free to see the answer and a step-by-step explanation.