public void union(int x, int y) {
    if (x == y) {
      return;
    }
    x = find(x);
    y = find(y);
    if (rank[x] < rank[y]) {
      root[x] = y;
      rank[y]++;
    } else {
      root[y] = x;
      rank[x]++;
    }
  }