| 16 | } |
| 17 | |
| 18 | bool unionSets(int x, int y) { |
| 19 | int repX = findSet(x); |
| 20 | int repY = findSet(y); |
| 21 | if (repX != repY) { |
| 22 | if (size[repX] > size[repY]) { |
| 23 | parent[repY] = repX; |
| 24 | size[repX] += size[repY]; |
| 25 | } else { |
| 26 | parent[repX] = repY; |
| 27 | size[repY] += size[repX]; |
| 28 | } |
| 29 | // Return true if both groups were merged. |
| 30 | return true; |
| 31 | } |
| 32 | // Return false if the points belong to the same group. |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | int findSet(int x) { |
| 37 | if (x == parent[x]) { |