code: public int[] stableMatching(int[][] manPref, int[][] womanPref, int menSize, int womenSize) {
  
      // Indicates the mating status
      int[] statusMan = new int[menSize];
      int[] statusWoman = new int[womenSize];
  
      final int NOT_ENGAGED = -1;
      for (int i = 0; i < womenSize; i++) {
        statusWoman[i] = NOT_ENGAGED;
      }
  
      // List of men that are not currently engaged.
      LinkedList<Integer> freeMen = new LinkedList<Integer>();
      for (int i = 0; i < menSize; i++) {
        freeMen.add(i);
      }
  
      // next[i] is the next woman to whom i has not yet proposed.
      int[] next = new int[womenSize];
  
      while (!freeMen.isEmpty()) {
        int m = freeMen.remove();
        int w = manPref[m][next[m]];
        next[m]++;
        if (statusWoman[w] == NOT_ENGAGED) {
          statusMan[m] = w;
          statusWoman[w] = m;
        } else {
          int m1 = statusWoman[w];
          if (prefers(m, m1, womanPref[w], menSize)) {
            statusMan[m] = w;
            statusWoman[w] = m;
            freeMen.add(m1);
          } else {
            freeMen.add(m);
          }
        }
      }
  
      return statusMan;
    }
docs: Return the stable matching between 'subproblems' and 'solutions' ('subproblems' propose first).
  It is worth noting that the number of solutions is larger than that of the subproblems.