Same find means connected
Updated
x and y are connected if and only if find(x) equals find(y). That comparison is the query. Distinct roots means disconnected. An index is connected to itself.
What to focus on
Compare representatives, not parent slots. parent[x] may differ from parent[y] while find still meets at one root.
Worked examples
merged pair
a = [0, 1, 2, 3], parent=[0,0,2,3], connected(0,1)- find 0=0
- find 1=0
Result: true
other root
a = [0, 1, 2, 3], parent=[0,0,2,3], connected(0,2)- find 0=0
- find 2=2
Result: false
deep child
a = [0, 1, 2, 3], parent=[0,0,1,3], connected(2,0)- 2 → 1 → 0
- parent[2]=1 ≠ parent[0]
- find both 0
Result: true
Try it
Write connected(parent, x, y). parent is the parent array. x and y are indices. Return true if find(x) equals find(y), otherwise return false. On parent=[0, 0, 2, 3], connected(0, 1) → true and connected(0, 2) → false. On parent=[0, 0, 1, 3], connected(2, 0) → true even though parent[2] is 1.
x and y are live indices. Every parent slot is valid. The forest already reflects the unions so far.