Problem p02942 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 998244353;
// 1000000007;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  ll i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}
template <typename T> vector<T> &cset(vector<T> &A, T e = T()) {
  for (auto &I : A) {
    I = e;
  }
  return A;
}

using Int = long long;
// BEGIN CUT HERE
template <typename T, bool directed> struct Dinic {
  struct edge {
    int to;
    T cap;
    int rev;
    edge() {}
    edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
  };

  vector<vector<edge>> G;
  vector<int> level, iter;

  Dinic() {}
  Dinic(int n) : G(n), level(n), iter(n) {}

  int add_edge(int from, int to, T cap) {
    G[from].emplace_back(to, cap, G[to].size());
    G[to].emplace_back(from, directed ? 0 : cap, G[from].size() - 1);
    return G[to].back().rev;
  }

  void bfs(int s) {
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < (int)G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.emplace(e.to);
        }
      }
    }
  }

  T dfs(int v, int t, T f) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        T d = dfs(e.to, t, min(f, e.cap));
        if (d == 0)
          continue;
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }

  T flow(int s, int t, T lim) {
    T fl = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0 || lim == 0)
        break;
      fill(iter.begin(), iter.end(), 0);

      while (1) {
        T f = dfs(s, t, lim);
        if (f == 0)
          break;
        fl += f;
        lim -= f;
      }
    }
    return fl;
  }

  T flow(int s, int t) { return flow(s, t, numeric_limits<T>::max() / 2); }

  T cut(int s, int t, int x, int a) {
    static_assert(directed, "must be directed");
    auto &e = G[x][a];
    int y = e.to;
    T ce = e.cap, cr = G[y][e.rev].cap;
    if (cr == 0)
      return 0;
    e.cap = G[y][e.rev].cap = 0;
    T cap = cr - flow(x, y, cr);
    if (x != s && cap != 0)
      flow(x, s, cap);
    if (t != y && cap != 0)
      flow(t, y, cap);
    e.cap = ce + cr;
    return cap;
  }
};
// END CUT HERE

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(M));
  vector<vector<int>> one(N, vector<int>(M));
  vector<vector<int>> cnt(N, vector<int>(N, 0));
  cin >> A;
  for (auto &I : A) {
    for (auto &J : I) {
      J--;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cnt[i][A[i][j] / M]++;
    }
  }
  for (int i = 0; i < M; i++) {
    Dinic<int, true> fl(2 + N + N);
    int s = 2 * N;
    int g = s + 1;
    int c = 0;
    int m = N;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        if (cnt[j][k]) {
          fl.add_edge(c + j, m + k, 1);
        }
      }
      fl.add_edge(s, c + j, 1);
      fl.add_edge(m + j, g, 1);
    }
    fl.flow(s, g);
    for (int j = 0; j < N; i++) {
      for (auto &e : fl.G[c + j]) {
        if (e.cap == 0 && e.to != s) {
          one[j][i] = e.to - m;
          cnt[j][e.to - m]--;
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      for (int k = 0; k < M; k++) {
        if (A[i][k] != -1 && A[i][k] / M == one[i][j]) {
          one[i][j] = A[i][k];
          A[i][k] = -1;
          break;
        }
      }
    }
  }
  for (auto &I : one) {
    for (auto &J : I) {
      J++;
    }
  }
  for (auto &I : one) {
    cout << I << endl;
  }
  for (int j = 0; j < M; j++) {
    vector<ll> X(N);
    for (int i = 0; i < N; i++) {
      X[i] = one[i][j];
    }
    sort(X.begin(), X.end());
    for (int i = 0; i < N; i++) {
      one[i][j] = X[i];
    }
  }
  for (auto &I : one) {
    cout << I << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 998244353;
// 1000000007;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  ll i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}
template <typename T> vector<T> &cset(vector<T> &A, T e = T()) {
  for (auto &I : A) {
    I = e;
  }
  return A;
}

using Int = long long;
// BEGIN CUT HERE
template <typename T, bool directed> struct Dinic {
  struct edge {
    int to;
    T cap;
    int rev;
    edge() {}
    edge(int to, T cap, int rev) : to(to), cap(cap), rev(rev) {}
  };

  vector<vector<edge>> G;
  vector<int> level, iter;

  Dinic() {}
  Dinic(int n) : G(n), level(n), iter(n) {}

  int add_edge(int from, int to, T cap) {
    G[from].emplace_back(to, cap, G[to].size());
    G[to].emplace_back(from, directed ? 0 : cap, G[from].size() - 1);
    return G[to].back().rev;
  }

  void bfs(int s) {
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < (int)G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[e.to] < 0) {
          level[e.to] = level[v] + 1;
          que.emplace(e.to);
        }
      }
    }
  }

  T dfs(int v, int t, T f) {
    if (v == t)
      return f;
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
        T d = dfs(e.to, t, min(f, e.cap));
        if (d == 0)
          continue;
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
    return 0;
  }

  T flow(int s, int t, T lim) {
    T fl = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0 || lim == 0)
        break;
      fill(iter.begin(), iter.end(), 0);

      while (1) {
        T f = dfs(s, t, lim);
        if (f == 0)
          break;
        fl += f;
        lim -= f;
      }
    }
    return fl;
  }

  T flow(int s, int t) { return flow(s, t, numeric_limits<T>::max() / 2); }

  T cut(int s, int t, int x, int a) {
    static_assert(directed, "must be directed");
    auto &e = G[x][a];
    int y = e.to;
    T ce = e.cap, cr = G[y][e.rev].cap;
    if (cr == 0)
      return 0;
    e.cap = G[y][e.rev].cap = 0;
    T cap = cr - flow(x, y, cr);
    if (x != s && cap != 0)
      flow(x, s, cap);
    if (t != y && cap != 0)
      flow(t, y, cap);
    e.cap = ce + cr;
    return cap;
  }
};
// END CUT HERE

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(M));
  vector<vector<int>> one(N, vector<int>(M));
  vector<vector<int>> cnt(N, vector<int>(N, 0));
  cin >> A;
  for (auto &I : A) {
    for (auto &J : I) {
      J--;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cnt[i][A[i][j] / M]++;
    }
  }
  for (int i = 0; i < M; i++) {
    Dinic<int, true> fl(2 + N + N);
    int s = 2 * N;
    int g = s + 1;
    int c = 0;
    int m = N;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        if (cnt[j][k]) {
          fl.add_edge(c + j, m + k, 1);
        }
      }
      fl.add_edge(s, c + j, 1);
      fl.add_edge(m + j, g, 1);
    }
    assert(fl.flow(s, g) == N);
    for (int j = 0; j < N; j++) {
      for (auto &e : fl.G[c + j]) {
        if (e.cap == 0 && e.to != s) {
          one[j][i] = e.to - m;
          cnt[j][e.to - m]--;
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      for (int k = 0; k < M; k++) {
        if (A[i][k] != -1 && A[i][k] / M == one[i][j]) {
          one[i][j] = A[i][k];
          A[i][k] = -1;
          break;
        }
      }
    }
  }
  for (auto &I : one) {
    for (auto &J : I) {
      J++;
    }
  }
  for (auto &I : one) {
    cout << I << endl;
  }
  for (int j = 0; j < M; j++) {
    vector<ll> X(N);
    for (int i = 0; i < N; i++) {
      X[i] = one[i][j];
    }
    sort(X.begin(), X.end());
    for (int i = 0; i < N; i++) {
      one[i][j] = X[i];
    }
  }
  for (auto &I : one) {
    cout << I << endl;
  }

  return 0;
}