Problem p02942 (C++)

Buggy submission (status: Runtime Error):
// ██████╗ ██╗   ██╗██╗      ██████╗ ███████╗
// ██╔══██╗██║   ██║██║      ██╔══██╗██╔════╝
// ██████╔╝██║   ██║██║█████╗██║  ██║█████╗
// ██╔══██╗██║   ██║██║╚════╝██║  ██║██╔══╝
// ██║  ██║╚██████╔╝██║      ██████╔╝███████╗
// ╚═╝  ╚═╝ ╚═════╝ ╚═╝      ╚═════╝ ╚══════╝
#include <bits/stdc++.h>

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

#define fst first
#define snd second
#define mp make_pair
// #define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define next ajksdslk
#define prev aklsfjk

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

const int maxn = (int)3e5 + 100;
const int maxlog = 21;
const int base = 1e9 + 7;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
// const int pp = 41;
const int inf = 2e9;
const ll llinf = 1e18;

vector<int> f[101][101];
int a[101][101];

int b[101][101];
int c[101][101];

bool used[101];
vector<int> g[101];
int mt[101];

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;
      return true;
    }
  }
  return false;
}

int main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      a[i][j]--;
      f[i][a[i][j] / m].pb(a[i][j]);
    }
  }

  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++)
      mt[i] = -1, g[i].clear();
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < n; k++) {
        if ((int)f[k][i].size()) {
          g[k].pb(i);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      memset(used, 0, sizeof(used));
      try_kuhn(i);
    }
    for (int i = 0; i < n; i++) {
      assert(mt[i] != -1);
      assert(f[i][mt[i]].size());
      b[i][j] = f[i][mt[i]].back();
      f[i][mt[i]].pop_back();
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << b[i][j] + 1 << ' ';
    }
    cout << endl;
  }

  // return 0;
  for (int i = 0; i < n; i++) {
    int fnd = 0;
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < n; k++) {
        if (b[k][j] / m == i) {
          c[i][j] = b[k][j];
          fnd++;
        }
      }
    }
    assert(fnd);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << c[i][j] + 1 << ' ';
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
// ██████╗ ██╗   ██╗██╗      ██████╗ ███████╗
// ██╔══██╗██║   ██║██║      ██╔══██╗██╔════╝
// ██████╔╝██║   ██║██║█████╗██║  ██║█████╗
// ██╔══██╗██║   ██║██║╚════╝██║  ██║██╔══╝
// ██║  ██║╚██████╔╝██║      ██████╔╝███████╗
// ╚═╝  ╚═╝ ╚═════╝ ╚═╝      ╚═════╝ ╚══════╝
#include <bits/stdc++.h>

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

#define fst first
#define snd second
#define mp make_pair
// #define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define next ajksdslk
#define prev aklsfjk

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

const int maxn = (int)3e5 + 100;
const int maxlog = 21;
const int base = 1e9 + 7;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
// const int pp = 41;
const int inf = 2e9;
const ll llinf = 1e18;

vector<int> f[101][101];
int a[101][101];

int b[101][101];
int c[101][101];

bool used[101];
vector<int> g[101];
int mt[101];

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;
      return true;
    }
  }
  return false;
}

int main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      a[i][j]--;
      f[i][a[i][j] / m].pb(a[i][j]);
    }
  }

  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++)
      mt[i] = -1, g[i].clear();
    for (int i = 0; i < n; i++) {
      for (int k = 0; k < n; k++) {
        if ((int)f[k][i].size()) {
          g[i].pb(k);
        }
      }
    }
    for (int i = 0; i < n; i++) {
      memset(used, 0, sizeof(used));
      try_kuhn(i);
    }
    for (int i = 0; i < n; i++) {
      assert(mt[i] != -1);
      assert(f[i][mt[i]].size());
      b[i][j] = f[i][mt[i]].back();
      f[i][mt[i]].pop_back();
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << b[i][j] + 1 << ' ';
    }
    cout << endl;
  }

  // return 0;
  for (int i = 0; i < n; i++) {
    int fnd = 0;
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < n; k++) {
        if (b[k][j] / m == i) {
          c[i][j] = b[k][j];
          fnd++;
        }
      }
    }
    assert(fnd);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << c[i][j] + 1 << ' ';
    }
    cout << endl;
  }
  return 0;
}