(x, y)
| 14 | x.rank = 0 |
| 15 | |
| 16 | def Union(x, y): |
| 17 | xRoot = Find(x) |
| 18 | yRoot = Find(y) |
| 19 | if xRoot.rank > yRoot.rank: |
| 20 | yRoot.parent = xRoot |
| 21 | elif xRoot.rank < yRoot.rank: |
| 22 | xRoot.parent = yRoot |
| 23 | elif xRoot != yRoot: # Unless x and y are already in same set, merge them |
| 24 | yRoot.parent = xRoot |
| 25 | xRoot.rank = xRoot.rank + 1 |
| 26 | |
| 27 | def Find(x): |
| 28 | if x.parent == x: |