Problem p02942 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 128
using namespace std;
int n, m;
vector<int> g[MAXN];
int a[MAXN][MAXN], b[MAXN][MAXN], us[MAXN][MAXN], used[MAXN], mt[MAXN];
void read() {
  int i, j;
  cin >> n >> m;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      cin >> a[i][j];
}

bool try_kuhn(int v) {
  if (used[v])
    return false;
  used[v] = true;
  for (size_t i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (mt[to] == -1 || try_kuhn(mt[to])) {
      mt[to] = v;
      mt[v] = to;
      return true;
    }
  }
  return false;
}

void solve() {
  int i, j, l;
  for (l = 0; l < m; l++) {
    // cout<<"---------\n";
    for (i = 0; i < n; i++)
      g[i].clear();
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (!us[i][j]) {
          g[i].push_back(a[i][j] / m - (a[i][j] % m == 0) + n);
          g[a[i][j] / m - (a[i][j] % m == 0) + n].push_back(i);
          // cout<<i<<" "<<a[i][j]/m-(a[i][j]%m==0)+n<<endl;
        }
      }
    }
    // cout<<"---------\n";
    memset(mt, -1, sizeof(mt));
    for (int v = 0; v < n; ++v) {

      memset(used, 0, sizeof(used));
      try_kuhn(v);
    }

    for (int i = 0; i < n; ++i) {
      // cout<<mt[i]<<endl;
      if (mt[i] != -1) {
        // printf ("%d %d\n", mt[i], i);
        for (j = 0; j < m; j++) {
          if (a[i][j] / m - (a[i][j] % m == 0) + n == mt[i] && !us[i][j]) {
            us[i][j] = true;
            /// cout<<" - > "<<i<<" "<<j<<endl;
            b[i][l] = a[i][j];
            break;
          }
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cout << b[i][j] << " ";
    }
    cout << endl;
  }
  vector<int> h;
  for (j = 0; j < m; j++) {
    h.clear();
    for (i = 0; i < n; i++) {
      h.push_back(b[i][j]);
    }
    sort(h.begin(), h.end());
    for (i = 0; i < n; i++) {
      b[i][j] = h[i];
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cout << b[i][j] << " ";
    }
    cout << endl;
  }
}

int main() {
  read();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MAXN 128 * 4
using namespace std;
int n, m;
vector<int> g[MAXN];
int a[MAXN][MAXN], b[MAXN][MAXN], us[MAXN][MAXN], used[MAXN], mt[MAXN];
void read() {
  int i, j;
  cin >> n >> m;
  for (i = 0; i < n; i++)
    for (j = 0; j < m; j++)
      cin >> a[i][j];
}

bool try_kuhn(int v) {
  if (used[v])
    return false;
  used[v] = true;
  for (size_t i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (mt[to] == -1 || try_kuhn(mt[to])) {
      mt[to] = v;
      mt[v] = to;
      return true;
    }
  }
  return false;
}

void solve() {
  int i, j, l;
  for (l = 0; l < m; l++) {
    // cout<<"---------\n";
    for (i = 0; i < n; i++)
      g[i].clear();
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (!us[i][j]) {
          g[i].push_back(a[i][j] / m - (a[i][j] % m == 0) + n);
          g[a[i][j] / m - (a[i][j] % m == 0) + n].push_back(i);
          // cout<<i<<" "<<a[i][j]/m-(a[i][j]%m==0)+n<<endl;
        }
      }
    }
    // cout<<"---------\n";
    memset(mt, -1, sizeof(mt));
    for (int v = 0; v < n; ++v) {

      memset(used, 0, sizeof(used));
      try_kuhn(v);
    }

    for (int i = 0; i < n; ++i) {
      // cout<<mt[i]<<endl;
      if (mt[i] != -1) {
        // printf ("%d %d\n", mt[i], i);
        for (j = 0; j < m; j++) {
          if (a[i][j] / m - (a[i][j] % m == 0) + n == mt[i] && !us[i][j]) {
            us[i][j] = true;
            /// cout<<" - > "<<i<<" "<<j<<endl;
            b[i][l] = a[i][j];
            break;
          }
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cout << b[i][j] << " ";
    }
    cout << endl;
  }
  vector<int> h;
  for (j = 0; j < m; j++) {
    h.clear();
    for (i = 0; i < n; i++) {
      h.push_back(b[i][j]);
    }
    sort(h.begin(), h.end());
    for (i = 0; i < n; i++) {
      b[i][j] = h[i];
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cout << b[i][j] << " ";
    }
    cout << endl;
  }
}

int main() {
  read();
  solve();
  return 0;
}