Problem p02942 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define XX first
#define YY second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
int M;
int A[114][114];
int vec[114][114];
set<int> unused[214];
using Tup = tuple<int, int, int>;
vector<Tup> eds;
map<int, pii> op[114];

void Invert(int v, int uc, int vc) {
  assert(op[v].count(uc));
  int w = v;
  vector<int> vs(1, w);
  vector<int> as;
  while (op[w].count(uc)) {
    int nw, a;
    tie(nw, a) = op[w][uc];
    vs.eb(nw);
    as.eb(a);
    swap(uc, vc);
    w = nw;
  }

  int m = as.size();
  assert(unused[vs.back()].count(uc));
  assert(!op[vs.back()].count(uc));
  unused[vs.back()].insert(vc);
  op[vs.back()].erase(vc);
  rrep(i, m) {
    int a = as[i];
    int u = vs[i];
    int v = vs[i + 1];
    op[u][uc] = pii(v, a);
    op[v][uc] = pii(u, a);
    unused[u].erase(uc);
    unused[v].erase(uc);
    swap(uc, vc);
  }
  assert(op[vs.front()].count(uc) && op[vs.front()].count(vc));
  op[vs.front()].erase(uc);
  unused[vs.front()].insert(uc);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  cin >> N >> M;
  rep(i, N) {
    rep(j, M) {
      cin >> A[i][j];
      int r = N + (A[i][j] - 1) / M;
      eds.eb(Tup(i, r, A[i][j]));
    }
  }

  rep(i, N) {
    rep(j, M) {
      unused[i].insert(j);
      unused[N + i].insert(j);
    }
  }

  for (Tup &p : eds) {
    int u, v, a;
    tie(u, v, a) = p;
    int uc = *unused[u].begin();
    if (!unused[v].count(uc)) {
      assert(unused[v].size());
      int vc = *unused[v].begin();
      Invert(v, uc, vc);
      assert(unused[u].count(uc));
      assert(unused[v].count(uc));
      assert(op[v].count(vc));
      assert(!unused[v].count(vc));
    }
    unused[u].erase(uc);
    unused[v].erase(uc);
    op[u][uc] = pii(v, a);
    op[v][uc] = pii(u, a);
    continue;
  }

  rep(i, N) {
    rep(j, M) {
      if (j)
        cout << " ";
      assert(op[i].count(j));
      int a = op[i][j].YY;
      cout << a;
      vec[j][i] = a;
    }
    cout << endl;
  }
  rep(j, M) { sort(vec[j], vec[j] + N); }
  rep(i, N) {
    rep(j, M) {
      if (j)
        cout << " ";
      cout << vec[j][i];
    }
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define XX first
#define YY second
#define pb emplace_back
#define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i))
#define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i))
#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define REP rep
#define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X))
#define all(X) (X).begin(), (X).end()
#define eb emplace_back

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
const ll MOD = 1e9 + 7;

#define rall(X) (X).rbegin(), (X).rend()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X))
#define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X))
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}

int N;
int M;
int A[114][114];
int vec[114][114];
set<int> unused[214];
using Tup = tuple<int, int, int>;
vector<Tup> eds;
map<int, pii> op[214];

void Invert(int v, int uc, int vc) {
  assert(op[v].count(uc));
  int w = v;
  vector<int> vs(1, w);
  vector<int> as;
  while (op[w].count(uc)) {
    int nw, a;
    tie(nw, a) = op[w][uc];
    vs.eb(nw);
    as.eb(a);
    swap(uc, vc);
    w = nw;
  }

  int m = as.size();
  assert(unused[vs.back()].count(uc));
  assert(!op[vs.back()].count(uc));
  unused[vs.back()].insert(vc);
  op[vs.back()].erase(vc);
  rrep(i, m) {
    int a = as[i];
    int u = vs[i];
    int v = vs[i + 1];
    op[u][uc] = pii(v, a);
    op[v][uc] = pii(u, a);
    unused[u].erase(uc);
    unused[v].erase(uc);
    swap(uc, vc);
  }
  assert(op[vs.front()].count(uc) && op[vs.front()].count(vc));
  op[vs.front()].erase(uc);
  unused[vs.front()].insert(uc);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(0);
  cin >> N >> M;
  rep(i, N) {
    rep(j, M) {
      cin >> A[i][j];
      int r = N + (A[i][j] - 1) / M;
      eds.eb(Tup(i, r, A[i][j]));
    }
  }

  rep(i, N) {
    rep(j, M) {
      unused[i].insert(j);
      unused[N + i].insert(j);
    }
  }

  for (Tup &p : eds) {
    int u, v, a;
    tie(u, v, a) = p;
    int uc = *unused[u].begin();
    if (!unused[v].count(uc)) {
      assert(unused[v].size());
      int vc = *unused[v].begin();
      Invert(v, uc, vc);
      assert(unused[u].count(uc));
      assert(unused[v].count(uc));
      assert(op[v].count(vc));
      assert(!unused[v].count(vc));
    }
    unused[u].erase(uc);
    unused[v].erase(uc);
    op[u][uc] = pii(v, a);
    op[v][uc] = pii(u, a);
    continue;
  }

  rep(i, N) {
    rep(j, M) {
      if (j)
        cout << " ";
      assert(op[i].count(j));
      int a = op[i][j].YY;
      cout << a;
      vec[j][i] = a;
    }
    cout << endl;
  }
  rep(j, M) { sort(vec[j], vec[j] + N); }
  rep(i, N) {
    rep(j, M) {
      if (j)
        cout << " ";
      cout << vec[j][i];
    }
    cout << endl;
  }
}