Problem p02942 (C++)

Buggy submission (status: Runtime Error):
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 100, inf = (1 << 29);

int Rr, Cc, Ps, Pt, Emat[N + 3][N + 3], Edep[N * 3], Eans[N + 3][N + 3];
bool Bb[N + 3][N + 3];
vector<int> Vec[N + 3];

struct GGRAPH {
  struct GNODE {
    int to, cap, Vx, Vy;
    GNODE *nxt, *rev;
  } Gnod[N * N];
  GNODE *Gtop[N * 3], *Gcnt;
  void Init(int Nn) {
    Gcnt = Gnod;
    fill(Gtop, Gtop + 1 + Nn, Gcnt);
  }
  void AddEdge(int u, int v, int ca1, int ca2, int Vx, int Vy) {
    GNODE *p = ++Gcnt, *q = ++Gcnt;
    *p = (GNODE){v, ca1, Vx, Vy, Gtop[u], q};
    Gtop[u] = p;
    *q = (GNODE){u, ca2, Vx, Vy, Gtop[v], p};
    Gtop[v] = q;
  }
} Gflw;

GGRAPH::GNODE *Cop[N * 3];

int Aug(int u, int flwin) {
  if (u == Pt)
    return flwin;
  int flwout = 0;
  for (auto &it = Cop[u]; it; it = it->nxt) {
    int v = it->to;
    if (!it->cap || Edep[v] != Edep[u] + 1)
      continue;
    int flwv = min(flwin - flwout, it->cap);
    flwv = Aug(v, flwv);
    flwout += flwv;
    it->cap -= flwv;
    it->rev->cap += flwv;
    if (flwout == flwin)
      break;
  }
  return flwout;
}
bool BFS() {
  for (int i = 1; i <= Rr * 2 + 2; i++)
    Cop[i] = Gflw.Gtop[i], Edep[i] = inf;
  queue<int> que;
  Edep[Ps] = 0;
  que.push(Ps);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (auto it = Cop[u]; it; it = it->nxt) {
      int v = it->to;
      if (!it->cap || Edep[v] != inf)
        continue;
      Edep[v] = Edep[u] + 1;
      if (v == Pt)
        return true;
      que.push(v);
    }
  }
  return false;
}
int Dinic() {
  int mxflw = 0;
  while (BFS())
    mxflw += Aug(Ps, inf);
  return mxflw;
}
int main() {
  scanf("%d%d", &Rr, &Cc);
  Ps = 2 * Rr + 1;
  Pt = 2 * Rr + 2;
  for (int i = 1; i <= Rr; i++)
    for (int j = 1; j <= Cc; j++)
      scanf("%d", &Emat[i][j]);
  for (int i = 1; i <= Cc; i++) {
    Gflw.Init(2 * Rr + 2);
    int Vtot = 0;
    for (int j = 1; j <= Rr; j++)
      for (int k = 1; k <= Cc; k++)
        if (!Bb[j][k]) {
          Gflw.AddEdge(j, (Emat[j][k] - 1) / Cc + 1 + Rr, 1, 0, j, k);
          Vtot += 2;
        }
    for (int j = 1; j <= Rr; j++)
      Gflw.AddEdge(Ps, j, 1, 0, 0, 0);
    for (int j = 1; j <= Rr; j++)
      Gflw.AddEdge(j + Rr, Pt, 1, 0, 0, 0);
    int Rs = Dinic();
    for (int j = 1; j <= Vtot; j += 2) {
      auto it = Gflw.Gnod[j];
      if (it.cap == 0) {
        Eans[it.Vx][i] = Emat[it.Vx][it.Vy];
        Bb[it.Vx][it.Vy] = true;
      }
    }
  }
  for (int i = 1; i <= Rr; i++)
    for (int j = 1; j <= Cc; j++) {
      printf("%d", Eans[i][j]);
      Vec[j].push_back(Eans[i][j]);
      if (j == Cc)
        printf("\n");
      else
        printf(" ");
    }
  for (int i = 1; i <= Cc; i++) {
    sort(Vec[i].begin(), Vec[i].end());
    for (int j = 1; j <= Rr; j++)
      Eans[j][i] = Vec[i][j - 1];
  }
  for (int i = 1; i <= Rr; i++)
    for (int j = 1; j <= Cc; j++) {
      printf("%d", Eans[i][j]);
      if (j == Cc)
        printf("\n");
      else
        printf(" ");
    }
  return 0;
}

Fixed submission:
/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 100, inf = (1 << 29);

int Rr, Cc, Ps, Pt, Emat[N + 3][N + 3], Edep[N * 3], Eans[N + 3][N + 3];
bool Bb[N + 3][N + 3];
vector<int> Vec[N + 3];

struct GGRAPH {
  struct GNODE {
    int to, cap, Vx, Vy;
    GNODE *nxt, *rev;
  } Gnod[(N * N + N * 2) * 3];
  GNODE *Gtop[N * 3], *Gcnt;
  void Init(int Nn) {
    Gcnt = Gnod;
    fill(Gtop, Gtop + 1 + Nn, Gcnt);
  }
  void AddEdge(int u, int v, int ca1, int ca2, int Vx, int Vy) {
    GNODE *p = ++Gcnt, *q = ++Gcnt;
    *p = (GNODE){v, ca1, Vx, Vy, Gtop[u], q};
    Gtop[u] = p;
    *q = (GNODE){u, ca2, Vx, Vy, Gtop[v], p};
    Gtop[v] = q;
  }
} Gflw;

GGRAPH::GNODE *Cop[N * 3];

int Aug(int u, int flwin) {
  if (u == Pt)
    return flwin;
  int flwout = 0;
  for (auto &it = Cop[u]; it; it = it->nxt) {
    int v = it->to;
    if (!it->cap || Edep[v] != Edep[u] + 1)
      continue;
    int flwv = min(flwin - flwout, it->cap);
    flwv = Aug(v, flwv);
    flwout += flwv;
    it->cap -= flwv;
    it->rev->cap += flwv;
    if (flwout == flwin)
      break;
  }
  return flwout;
}
bool BFS() {
  for (int i = 1; i <= Rr * 2 + 2; i++)
    Cop[i] = Gflw.Gtop[i], Edep[i] = inf;
  queue<int> que;
  Edep[Ps] = 0;
  que.push(Ps);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (auto it = Cop[u]; it; it = it->nxt) {
      int v = it->to;
      if (!it->cap || Edep[v] != inf)
        continue;
      Edep[v] = Edep[u] + 1;
      if (v == Pt)
        return true;
      que.push(v);
    }
  }
  return false;
}
int Dinic() {
  int mxflw = 0;
  while (BFS())
    mxflw += Aug(Ps, inf);
  return mxflw;
}
int main() {
  scanf("%d%d", &Rr, &Cc);
  Ps = 2 * Rr + 1;
  Pt = 2 * Rr + 2;
  for (int i = 1; i <= Rr; i++)
    for (int j = 1; j <= Cc; j++)
      scanf("%d", &Emat[i][j]);
  for (int i = 1; i <= Cc; i++) {
    Gflw.Init(2 * Rr + 2);
    int Vtot = 0;
    for (int j = 1; j <= Rr; j++)
      for (int k = 1; k <= Cc; k++)
        if (!Bb[j][k]) {
          Gflw.AddEdge(j, (Emat[j][k] - 1) / Cc + 1 + Rr, 1, 0, j, k);
          Vtot += 2;
        }
    for (int j = 1; j <= Rr; j++)
      Gflw.AddEdge(Ps, j, 1, 0, 0, 0);
    for (int j = 1; j <= Rr; j++)
      Gflw.AddEdge(j + Rr, Pt, 1, 0, 0, 0);
    int Rs = Dinic();
    for (int j = 1; j <= Vtot; j += 2) {
      auto it = Gflw.Gnod[j];
      if (it.cap == 0) {
        Eans[it.Vx][i] = Emat[it.Vx][it.Vy];
        Bb[it.Vx][it.Vy] = true;
      }
    }
  }
  for (int i = 1; i <= Rr; i++)
    for (int j = 1; j <= Cc; j++) {
      printf("%d", Eans[i][j]);
      Vec[j].push_back(Eans[i][j]);
      if (j == Cc)
        printf("\n");
      else
        printf(" ");
    }
  for (int i = 1; i <= Cc; i++) {
    sort(Vec[i].begin(), Vec[i].end());
    for (int j = 1; j <= Rr; j++)
      Eans[j][i] = Vec[i][j - 1];
  }
  for (int i = 1; i <= Rr; i++)
    for (int j = 1; j <= Cc; j++) {
      printf("%d", Eans[i][j]);
      if (j == Cc)
        printf("\n");
      else
        printf(" ");
    }
  return 0;
}