Problem p02942 (C++)

Buggy submission (status: Runtime Error):
// Love and Freedom.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define ll long long
#define inf 20021225
#define N 210
#define M 20001
using namespace std;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return f * s;
}
struct edge {
  int to, lt, f;
} e[M];
int in[N], cnt, s, t, n, m; // clear!
void add(int x, int y, int f) {
  e[++cnt].to = y;
  e[cnt].lt = in[x];
  in[x] = cnt;
  e[cnt].f = f;
  e[++cnt].to = x;
  e[cnt].lt = in[y];
  in[y] = cnt;
  e[cnt].f = 0;
}
queue<int> que;
int dep[N];
bool bfs() {
  // printf("GG");
  while (!que.empty())
    que.pop();
  memset(dep, 0, sizeof(dep));
  que.push(s);
  dep[s] = 1;
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    for (int i = in[x]; i; i = e[i].lt)
      if (!dep[e[i].to] && e[i].f) {
        dep[e[i].to] = dep[x] + 1, que.push(e[i].to);
        if (e[i].to == t)
          return 1;
      }
  }
  return 0;
}
int dfs(int x, int flow) {
  if (x == t)
    return flow;
  int cur = flow;
  for (int i = in[x]; i; i = e[i].lt) {
    int y = e[i].to;
    if (dep[y] == dep[x] + 1 && e[i].f) {
      int tmp = dfs(y, min(cur, e[i].f)); // printf("GG");
      cur -= tmp;
      e[i].f -= tmp;
      e[i ^ 1].f += tmp;
      if (!cur)
        return flow;
    }
  }
  dep[x] = -1;
  return flow - cur;
}
vector<int> val[N][N];
int sz[N][N];
void link() {
  cnt = 1;
  s = N - 3;
  t = s + 1;
  memset(in, 0, sizeof(in));
  for (int i = 1; i <= n; i++) {
    add(s, i, 1);
    for (int j = 1; j <= n; j++)
      if (sz[i][j] < val[i][j].size())
        add(i, n + j, 1);
  }
  for (int i = 1; i <= n; i++)
    add(n + i, t, 1);
}
int r[N], b[N][N], a[N][N];
void dinic() {
  // printf("QAQ");
  while (bfs())
    dfs(s, inf);
  // printf("QwQ");
  for (int i = 1; i <= n; i++)
    for (int j = in[i]; j; j = e[j].lt) {
      if (e[j].to != s && e[j ^ 1].f) {
        r[i] = e[j].to - n; // printf("%d %d\n",i,r[i]);
        break;
      }
    }
  // printf("QAQ");
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      a[i][j] = read();
      int id = (a[i][j] - 1) / m + 1;
      val[i][id].push_back(a[i][j]);
      // printf("%d %d\n",i,id);
    }
  // printf("OVO");
  for (int i = 1; i <= m; i++) {
    link();
    dinic(); // printf("GG");
    for (int j = 1; j <= n; j++)
      b[j][i] = val[j][r[j]][sz[j][r[j]]++];
    // printf("QAQ");
  }
  for (int i = 1; i <= n; i++, printf("\n"))
    for (int j = 1; j <= m; j++)
      printf("%d ", b[i][j]), a[j][i] = b[i][j];
  for (int i = 1; i <= m; i++)
    sort(a[i] + 1, a[i] + n + 1);
  for (int i = 1; i <= n; i++, printf("\n"))
    for (int j = 1; j <= m; j++)
      printf("%d ", a[j][i]);
  return 0;
}

Fixed submission:
// Love and Freedom.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define ll long long
#define inf 20021225
#define N 410
#define M 40001
using namespace std;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return f * s;
}
struct edge {
  int to, lt, f;
} e[M];
int in[N], cnt, s, t, n, m; // clear!
void add(int x, int y, int f) {
  e[++cnt].to = y;
  e[cnt].lt = in[x];
  in[x] = cnt;
  e[cnt].f = f;
  e[++cnt].to = x;
  e[cnt].lt = in[y];
  in[y] = cnt;
  e[cnt].f = 0;
}
queue<int> que;
int dep[N];
bool bfs() {
  // printf("GG");
  while (!que.empty())
    que.pop();
  memset(dep, 0, sizeof(dep));
  que.push(s);
  dep[s] = 1;
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    for (int i = in[x]; i; i = e[i].lt)
      if (!dep[e[i].to] && e[i].f) {
        dep[e[i].to] = dep[x] + 1, que.push(e[i].to);
        if (e[i].to == t)
          return 1;
      }
  }
  return 0;
}
int dfs(int x, int flow) {
  if (x == t)
    return flow;
  int cur = flow;
  for (int i = in[x]; i; i = e[i].lt) {
    int y = e[i].to;
    if (dep[y] == dep[x] + 1 && e[i].f) {
      int tmp = dfs(y, min(cur, e[i].f)); // printf("GG");
      cur -= tmp;
      e[i].f -= tmp;
      e[i ^ 1].f += tmp;
      if (!cur)
        return flow;
    }
  }
  dep[x] = -1;
  return flow - cur;
}
vector<int> val[N][N];
int sz[N][N];
void link() {
  cnt = 1;
  s = N - 3;
  t = s + 1;
  memset(in, 0, sizeof(in));
  for (int i = 1; i <= n; i++) {
    add(s, i, 1);
    for (int j = 1; j <= n; j++)
      if (sz[i][j] < val[i][j].size())
        add(i, n + j, 1);
  }
  for (int i = 1; i <= n; i++)
    add(n + i, t, 1);
}
int r[N], b[N][N], a[N][N];
void dinic() {
  // printf("QAQ");
  while (bfs())
    dfs(s, inf);
  // printf("QwQ");
  for (int i = 1; i <= n; i++)
    for (int j = in[i]; j; j = e[j].lt) {
      if (e[j].to != s && e[j ^ 1].f) {
        r[i] = e[j].to - n; // printf("%d %d\n",i,r[i]);
        break;
      }
    }
  // printf("QAQ");
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      a[i][j] = read();
      int id = (a[i][j] - 1) / m + 1;
      val[i][id].push_back(a[i][j]);
      // printf("%d %d\n",i,id);
    }
  // printf("OVO");
  for (int i = 1; i <= m; i++) {
    link();
    dinic(); // printf("GG");
    for (int j = 1; j <= n; j++)
      b[j][i] = val[j][r[j]][sz[j][r[j]]++];
    // printf("QAQ");
  }
  for (int i = 1; i <= n; i++, printf("\n"))
    for (int j = 1; j <= m; j++)
      printf("%d ", b[i][j]), a[j][i] = b[i][j];
  for (int i = 1; i <= m; i++)
    sort(a[i] + 1, a[i] + n + 1);
  for (int i = 1; i <= n; i++, printf("\n"))
    for (int j = 1; j <= m; j++)
      printf("%d ", a[j][i]);
  return 0;
}