deffind(self, x): for i, s inenumerate(self.all_set): if x in s: return i new_set = set([x]) self.all_set.add(new_set) returnlen(self.all_set) - 1
defunion(self, x, y): xi = self.find(x) yi = self.find(y) if xi == yi: return xi elif xi < yi: Y = self.all_set.pop(yi) X = self.all_set.pop(xi) else: X = self.all_set.pop(xi) Y = self.all_set.pop(yi) self.all_set.append(X | Y) returnlen(self.all_set) - 1
deffind(self, x): if x notin self.val2id: self.val2id[x] = self.n self.id2vallist[self.n] = [x] self.n += 1 return self.val2id[x]
defunion(self, x, y): xi = self.find(x) yi = self.find(y) if xi != yi: listy = self.id2vallist.pop(yi) for i in listy: self.val2id[i] = xi self.id2vallist[xi].extend(listy) return xi