Question DSA-GRAPH-0015
HardWhat is the output of this code?
parent = list(range(5))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for a, b in [(0, 1), (1, 2), (3, 4)]:
parent[find(a)] = find(b)
print(len({find(x) for x in range(5)}))
- A.
4
- B.
3
- C.
Sign up free to see the answer and a step-by-step explanation.