Problem p02942 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MN 210
#define MM 20100

int h[MN], nxt[MM], to[MM], cap[MM], K = 1;
int q[MN], hh = 0, tt = 0;
int iter[MN], d[MN];
int A[MN][MN], B[MN][MN], C[MN][MN];
std::vector<int> cnt[MN][MN];

void ins(int u, int v, int c) {
  nxt[++K] = h[u];
  h[u] = K;
  to[K] = v;
  cap[K] = c;
}
void insw(int u, int v, int c) {
  ins(u, v, c);
  ins(v, u, 0);
}

bool bfs(int S, int T) {
  memcpy(iter, h, sizeof(h));
  memset(d, 0, sizeof(d));
  d[S] = 1;
  hh = tt = 0;
  q[tt++] = S;
  while (hh < tt) {
    int u = q[hh++];
    for (int i = h[u]; i; i = nxt[i])
      if (cap[i] && !d[to[i]]) {
        d[to[i]] = d[u] + 1;
        q[tt++] = to[i];
      }
  }
  return d[T];
}

int dfs(int u, int T, int f) {
  if (u == T)
    return f;
  int used = 0;
  for (int &i = iter[u]; i; i = nxt[i]) {
    if (cap[i] && d[to[i]] == d[u] + 1) {
      int w = dfs(to[i], T, std::min(cap[i], f - used));
      if (w) {
        cap[i] -= w;
        cap[i ^ 1] += w;
        used += w;
        if (f == used)
          return f;
      }
    }
  }
  return used;
}

int dinic(int S, int T) {
  int w, f = 0;
  while (bfs(S, T))
    while (w = dfs(S, T, 1e9))
      f += w;
  return f;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &A[i][j]);
      cnt[i][(A[i][j] - 1) / m + 1].push_back(A[i][j]);
    }
  int S = 2 * n + 1, T = 2 * n + 2;
  for (int i = 1; i <= m; i++) {
    memset(h, 0, sizeof(h));
    K = 1;
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        if (cnt[j][k].size())
          insw(j, n + k, 1);
    for (int j = 1; j <= n; j++)
      insw(S, j, 1), insw(j + n, T, 1);
    dinic(S, T);
    for (int j = 1; j <= n; j++)
      for (int x = h[j]; x; x = nxt[x])
        if (!cap[x] && n + 1 <= to[x] && to[x] <= 2 * n)
          B[j][i] = cnt[j][to[x] - n].back(), cnt[j][to[x] - n].pop_back();
  }
  for (int i = 1; i <= n; i++, puts(""))
    for (int j = 1; j <= m; j++) {
      C[(B[i][j] - 1) / m + 1][j] = B[i][j];
      printf("%d ", B[i][j]);
    }
  for (int i = 1; i <= n; i++, puts(""))
    for (int j = 1; j <= m; j++)
      printf("%d ", C[i][j]);
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MN 210
#define MM 21000

int h[MN], nxt[MM], to[MM], cap[MM], K = 1;
int q[MN], hh = 0, tt = 0;
int iter[MN], d[MN];
int A[MN][MN], B[MN][MN], C[MN][MN];
std::vector<int> cnt[MN][MN];

void ins(int u, int v, int c) {
  nxt[++K] = h[u];
  h[u] = K;
  to[K] = v;
  cap[K] = c;
}
void insw(int u, int v, int c) {
  ins(u, v, c);
  ins(v, u, 0);
}

bool bfs(int S, int T) {
  memcpy(iter, h, sizeof(h));
  memset(d, 0, sizeof(d));
  d[S] = 1;
  hh = tt = 0;
  q[tt++] = S;
  while (hh < tt) {
    int u = q[hh++];
    for (int i = h[u]; i; i = nxt[i])
      if (cap[i] && !d[to[i]]) {
        d[to[i]] = d[u] + 1;
        q[tt++] = to[i];
      }
  }
  return d[T];
}

int dfs(int u, int T, int f) {
  if (u == T)
    return f;
  int used = 0;
  for (int &i = iter[u]; i; i = nxt[i]) {
    if (cap[i] && d[to[i]] == d[u] + 1) {
      int w = dfs(to[i], T, std::min(cap[i], f - used));
      if (w) {
        cap[i] -= w;
        cap[i ^ 1] += w;
        used += w;
        if (f == used)
          return f;
      }
    }
  }
  return used;
}

int dinic(int S, int T) {
  int w, f = 0;
  while (bfs(S, T))
    while (w = dfs(S, T, 1e9))
      f += w;
  return f;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf("%d", &A[i][j]);
      cnt[i][(A[i][j] - 1) / m + 1].push_back(A[i][j]);
    }
  int S = 2 * n + 1, T = 2 * n + 2;
  for (int i = 1; i <= m; i++) {
    memset(h, 0, sizeof(h));
    K = 1;
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        if (cnt[j][k].size())
          insw(j, n + k, 1);
    for (int j = 1; j <= n; j++)
      insw(S, j, 1), insw(j + n, T, 1);
    dinic(S, T);
    for (int j = 1; j <= n; j++)
      for (int x = h[j]; x; x = nxt[x])
        if (!cap[x] && n + 1 <= to[x] && to[x] <= 2 * n)
          B[j][i] = cnt[j][to[x] - n].back(), cnt[j][to[x] - n].pop_back();
  }
  for (int i = 1; i <= n; i++, puts(""))
    for (int j = 1; j <= m; j++) {
      C[(B[i][j] - 1) / m + 1][j] = B[i][j];
      printf("%d ", B[i][j]);
    }
  for (int i = 1; i <= n; i++, puts(""))
    for (int j = 1; j <= m; j++)
      printf("%d ", C[i][j]);
}