| 33 | return True |
| 34 | |
| 35 | class union_set: |
| 36 | def __init__(self, count): |
| 37 | self._obj = [x for x in range(0, count)] |
| 38 | |
| 39 | def find(self, x): |
| 40 | if x != self._obj[x]: |
| 41 | self._obj[x] = self.find(self._obj[x]) |
| 42 | return self._obj[x] |
| 43 | else: |
| 44 | return x |
| 45 | |
| 46 | def connect(self, a, b): |
| 47 | x = self.find(a) |
| 48 | y = self.find(b) |
| 49 | self._obj[x] = y |
| 50 | |
| 51 | def is_connected(self, a, b): |
| 52 | return self.find(a) == self.find(b) |
| 53 | |
| 54 | # find best indices |
| 55 | # !!NEED TO BE IMPROVED!! |