Design a data structure that stores a partition of a set.
With the operations of union, find, etc.
If two elements are in the same set, then the head of both are the same.
If djs[x]
is negative, then x
is the head of the set,
and -djs[x]
is the population of the set.
Otherwise, djs[x]
is also a member(it's usually head) of the set.
If djs[x]
is negative, then x
is the head of the set.
Otherwise, replace djs[x]
with find(djs[x])
. (Path compression)
Compare djs[find(x)]
with djs[find(y)]
,
adding the smaller set to the other one. (Union by Rank)