parent
to represent each node's parent, with each node initially being its own parent.find
function that uses path compression to find the root parent of a node.(u, v)
, apply the find
function to both u
and v
.find(u)
equals find(v)
, then a cycle is detected, and the current edge is the redundant one.union
operation to merge the subsets containing u
and v
.