Union function
| 31 | |
| 32 | // Union function |
| 33 | void unite(int x, int y) |
| 34 | { |
| 35 | int s1 = find(x); |
| 36 | int s2 = find(y); |
| 37 | |
| 38 | if (s1 != s2) { |
| 39 | if (rank[s1] < rank[s2]) { |
| 40 | parent[s1] = s2; |
| 41 | rank[s2] += rank[s1]; |
| 42 | } |
| 43 | else { |
| 44 | parent[s2] = s1; |
| 45 | rank[s1] += rank[s2]; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | class Graph { |