Problem p02378 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int mx = 305;
const int inf = 0x3f3f3f3f;
const int S = mx - 2, T = mx - 1;

struct edge {
  int from, to, cap;
  edge *rev;
  edge(int from, int to, int cap) : from(from), to(to), cap(cap) {}
  void flow(int f) { cap -= f, rev->cap += f; }
};

vector<edge *> adj[mx];

void addEdge(int u, int v, int c) {
  edge *e = new edge(u, v, c);
  edge *rev = new edge(v, u, 0);
  e->rev = rev, rev->rev = e;
  adj[u].push_back(e);
  adj[v].push_back(rev);
}

bool vis[mx], inq[mx];
int bt[mx], mf[mx];
edge *bte[mx];
bool augment(int &flow) {
  fill(vis, vis + mx, false);
  fill(inq, inq + mx, false);
  fill(mf, mf + mx, 0);
  queue<int> q;
  q.push(S);
  mf[S] = inf;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    inq[p] = false;
    if (vis[p])
      continue;
    // printf("Visit: %d\\n", p);
    vis[p] = true;
    if (p == T)
      break;
    for (edge *e : adj[p]) {
      // printf("Edge: %d -> %d, cap %d\\n", e->from, e->to, e->cap);
      if (!vis[e->to] && min(e->cap, mf[p]) > mf[e->to]) {
        bt[e->to] = p;
        bte[e->to] = e;
        mf[e->to] = min(e->cap, mf[p]);
        if (!inq[e->to])
          q.push(e->to), inq[e->to] = true;
      }
    }
  }
  // printf("Done, vis: %d\\n", vis[T]);
  if (!vis[T])
    return false;
  int p = T, f = mf[T];
  while (p != S) {
    bte[p]->flow(f);
    p = bt[p];
  }
  flow = f;
  return true;
}

int maxflow() {
  int flow, mflow = 0;
  while (augment(flow))
    mflow += flow;
  return mflow;
}

int main() {
  int x, y, e;
  cin >> x >> y >> e;

  for (int i = 0; i < x; i++)
    addEdge(S, i, 1);
  for (int i = 100; i < 100 + y; i++)
    addEdge(i, T, 1);

  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    addEdge(a, 100 + b, inf);
  }

  cout << maxflow() << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int mx = 305;
const int inf = 0x3f3f3f3f;
const int S = mx - 2, T = mx - 1;

struct edge {
  int from, to, cap;
  edge *rev;
  edge(int from, int to, int cap) : from(from), to(to), cap(cap) {}
  void flow(int f) { cap -= f, rev->cap += f; }
};

vector<edge *> adj[mx];

void addEdge(int u, int v, int c) {
  edge *e = new edge(u, v, c);
  e->rev = new edge(v, u, 0);
  e->rev->rev = e;
  adj[u].push_back(e);
  adj[v].push_back(e->rev);
}

bool vis[mx], inq[mx];
int bt[mx], mf[mx];
edge *bte[mx];
bool augment(int &flow) {
  fill(vis, vis + mx, false);
  fill(inq, inq + mx, false);
  fill(mf, mf + mx, 0);
  queue<int> q;
  q.push(S);
  mf[S] = inf;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    inq[p] = false;
    if (vis[p])
      continue;
    // printf("Visit: %d\\n", p);
    vis[p] = true;
    if (p == T)
      break;
    for (edge *e : adj[p]) {
      // printf("Edge: %d -> %d, cap %d\\n", e->from, e->to, e->cap);
      if (!vis[e->to] && min(e->cap, mf[p]) > mf[e->to]) {
        bt[e->to] = p;
        bte[e->to] = e;
        mf[e->to] = min(e->cap, mf[p]);
        if (!inq[e->to])
          q.push(e->to), inq[e->to] = true;
      }
    }
  }
  // printf("Done, vis: %d\\n", vis[T]);
  if (!vis[T])
    return false;
  int p = T, f = mf[T];
  while (p != S) {
    bte[p]->flow(f);
    p = bt[p];
  }
  flow = f;
  return true;
}

int maxflow() {
  int flow, mflow = 0;
  while (augment(flow))
    mflow += flow;
  return mflow;
}

int main() {
  int x, y, e;
  cin >> x >> y >> e;

  for (int i = 0; i < x; i++)
    addEdge(S, i, 1);
  for (int i = 100; i < 100 + y; i++)
    addEdge(i, T, 1);

  for (int i = 0; i < e; i++) {
    int a, b;
    cin >> a >> b;
    addEdge(a, 100 + b, inf);
  }

  cout << maxflow() << endl;
}