posts: union-find: fix union of same root
All checks were successful
ci/woodpecker/push/deploy/2 Pipeline was successful
ci/woodpecker/cron/deploy/2 Pipeline was successful

This commit is contained in:
Bruno BELANYI 2024-06-27 13:16:44 +00:00
parent adc514215c
commit 7873828c4c

View file

@ -142,6 +142,9 @@ _approximate_ height, we can keep the trees balanced when merging them.
def union(self, lhs: int, rhs: int) -> int:
lhs = self.find(lhs)
rhs = self.find(rhs)
# Bail out early if they already belong to the same set
if lhs == rhs:
return lhs
# Always keep `lhs` as the taller tree
if (self._rank[lhs] < self._rank[rhs])
lhs, rhs = rhs, lhs