Problem p02942 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define f(a, b, c) for (int a = (b); a <= (c); ++a)
const int maxn = 2e4 + 1;
int he[maxn], ne[maxn], to[maxn], w[maxn], cnt = 1;
inline void addedge(int x, int y, int z) {
  ne[++cnt] = he[x], he[x] = cnt, to[cnt] = y, w[cnt] = z;
  ne[++cnt] = he[y], he[y] = cnt, to[cnt] = x, w[cnt] = 0;
}
int S, T;
int dis[maxn], inq[maxn];
int bfs() {
  f(i, 1, T) inq[i] = dis[i] = 0;
  queue<int> q;
  q.push(S), dis[S] = 1;
  while (q.size()) {
    int x = q.front();
    q.pop();
    inq[x] = 0;
    for (int i = he[x]; i; i = ne[i]) {
      if (w[i] && !dis[to[i]]) {
        dis[to[i]] = dis[x] + 1;
        if (!inq[to[i]]) {
          inq[to[i]] = 1;
          q.push(to[i]);
        }
      }
    }
  }
  return dis[T];
}
int cur[maxn];
int dfs(int x, int f) {
  if (f == 0 || x == T)
    return f;
  int ans = 0;
  for (int i = cur[x]; i && ans < f; i = ne[i]) {
    cur[x] = i;
    if (dis[x] + 1 == dis[to[i]] && w[i]) {
      int qwq = dfs(to[i], min(f - ans, w[i]));
      ans += qwq;
      w[i] -= qwq;
      w[i ^ 1] += qwq;
    }
  }
  return ans;
}
int dinic() {
  int ans = 0;
  while (bfs()) {
    f(i, 1, T) cur[i] = he[i];
    ans += dfs(S, 1e9);
  }
  return ans;
}
vector<int> c[101][101];
int n, m;
int a[101][101];
int e[101][101];
int b[101][101];
void clear() {
  cnt = 1;
  f(i, 1, T) he[i] = 0;
}
int qwq[101];
int main() {
  scanf("%d%d", &n, &m);
  f(i, 1, n) f(j, 1, m) scanf("%d", &a[i][j]),
      c[i][(a[i][j] - 1) / m + 1].push_back(a[i][j]);
  S = n + n + 1, T = S + 1;
  f(k, 1, m) {
    //    	cout<<k<<":\n";
    f(i, 1, n) f(j, 1, n) {
      e[i][j] = -1;
      if (c[i][j].size()) {
        addedge(i, n + j, 1);
        e[i][j] = cnt - 1;
        //    			cout<<i<<" "<<j<<"\n";
      }
    }
    f(i, 1, n) {
      addedge(S, i, 1);
      addedge(i + n, T, 1);
    }
    dinic();
    f(i, 1, n) f(j, 1, n) {
      if (e[i][j] >= 0 && w[e[i][j]] == 0) {
        b[i][k] = c[i][j][c[i][j].size() - 1];
        c[i][j].pop_back();
        break;
      }
    }
    clear();
  }
  f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
  f(j, 1, m) {
    f(i, 1, n) qwq[i] = b[i][j];
    sort(qwq + 1, qwq + n + 1);
    f(i, 1, n) b[i][j] = qwq[i];
  }
  f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define f(a, b, c) for (int a = (b); a <= (c); ++a)
const int maxn = 3e4 + 2;
int he[maxn], ne[maxn], to[maxn], w[maxn], cnt = 1;
inline void addedge(int x, int y, int z) {
  ne[++cnt] = he[x], he[x] = cnt, to[cnt] = y, w[cnt] = z;
  ne[++cnt] = he[y], he[y] = cnt, to[cnt] = x, w[cnt] = 0;
}
int S, T;
int dis[maxn], inq[maxn];
int bfs() {
  f(i, 1, T) inq[i] = dis[i] = 0;
  queue<int> q;
  q.push(S), dis[S] = 1;
  while (q.size()) {
    int x = q.front();
    q.pop();
    inq[x] = 0;
    for (int i = he[x]; i; i = ne[i]) {
      if (w[i] && !dis[to[i]]) {
        dis[to[i]] = dis[x] + 1;
        if (!inq[to[i]]) {
          inq[to[i]] = 1;
          q.push(to[i]);
        }
      }
    }
  }
  return dis[T];
}
int cur[maxn];
int dfs(int x, int f) {
  if (f == 0 || x == T)
    return f;
  int ans = 0;
  for (int i = cur[x]; i && ans < f; i = ne[i]) {
    cur[x] = i;
    if (dis[x] + 1 == dis[to[i]] && w[i]) {
      int qwq = dfs(to[i], min(f - ans, w[i]));
      ans += qwq;
      w[i] -= qwq;
      w[i ^ 1] += qwq;
    }
  }
  return ans;
}
int dinic() {
  int ans = 0;
  while (bfs()) {
    f(i, 1, T) cur[i] = he[i];
    ans += dfs(S, 1e9);
  }
  return ans;
}
vector<int> c[101][101];
int n, m;
int a[101][101];
int e[101][101];
int b[101][101];
void clear() {
  cnt = 1;
  f(i, 1, T) he[i] = 0;
}
int qwq[101];
int main() {
  scanf("%d%d", &n, &m);
  f(i, 1, n) f(j, 1, m) scanf("%d", &a[i][j]),
      c[i][(a[i][j] - 1) / m + 1].push_back(a[i][j]);
  S = n + n + 1, T = S + 1;
  f(k, 1, m) {
    //    	cout<<k<<":\n";
    f(i, 1, n) f(j, 1, n) {
      e[i][j] = -1;
      if (c[i][j].size()) {
        addedge(i, n + j, 1);
        e[i][j] = cnt - 1;
        //    			cout<<i<<" "<<j<<"\n";
      }
    }
    f(i, 1, n) {
      addedge(S, i, 1);
      addedge(i + n, T, 1);
    }
    dinic();
    f(i, 1, n) f(j, 1, n) {
      if (e[i][j] >= 0 && w[e[i][j]] == 0) {
        b[i][k] = c[i][j][c[i][j].size() - 1];
        c[i][j].pop_back();
        break;
      }
    }
    clear();
  }
  f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
  f(j, 1, m) {
    f(i, 1, n) qwq[i] = b[i][j];
    sort(qwq + 1, qwq + n + 1);
    f(i, 1, n) b[i][j] = qwq[i];
  }
  f(i, 1, n) f(j, 1, m) printf("%d%c", b[i][j], " \n"[j == m]);
  return 0;
}