n
nodes, each node in its own set.(u, v)
, perform find
operation on both u
and v
.find(u)
equals find(v)
, a cycle is detected, return false
.find(u)
does not equal find(v)
, merge the sets containing u
and v
using union
operation.true
, otherwise return false
.n - 1
. If not, return false
.