| 14 | } |
| 15 | |
| 16 | public void union(int x, int y) { |
| 17 | int repX = find(x); |
| 18 | int repY = find(y); |
| 19 | if (repX != repY) { |
| 20 | // If 'repX' represents a larger community, connect |
| 21 | // 'repY 's community to it. |
| 22 | if (this.size[repX] > this.size[repY]) { |
| 23 | this.parent[repY] = repX; |
| 24 | this.size[repX] += this.size[repY]; |
| 25 | } |
| 26 | // Otherwise, connect 'rep_x's community to 'rep_y'. |
| 27 | else { |
| 28 | this.parent[repX] = repY; |
| 29 | this.size[repY] += this.size[repX]; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public int find(int x) { |
| 35 | if (x == this.parent[x]) { |