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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

struct max_flow {
  vector<vector<int>> graph;
  vector<bool> done;
  int size, flow;
  max_flow(int size_)
      : size(size_), graph(size_, vector<int>(size_)), done(size_), flow(0) {}
  void add_edge(int from, int to, int cap) {
    graph[from][to] = graph[to][from] = cap;
  }
  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    done[v] = true;
    for (int w = 0; w < size; ++w) {
      if (done[w] or graph[v][w] == 0)
        continue;
      int d = dfs(w, t, min(f, graph[v][w]));
      if (d > 0) {
        graph[v][w] -= d;
        graph[w][v] += d;
        return d;
      }
    }
    return 0;
  }
  int calc_max_flow(int s, int t) {
    while (true) {
      fill(done.begin(), done.end(), false);
      int f = dfs(s, t, inf);
      if (f == 0)
        return flow;
      flow += f;
    }
  }
};

void solve() {
  int n, e, q;
  cin >> n >> e >> q;
  max_flow mf(n);
  rep(i, 0, e) {
    int f, t;
    cin >> f >> t;
    --f;
    --t;
    mf.add_edge(f, t, 1);
  }
  mf.calc_max_flow(0, n - 1);
  auto search_path = [&](int s, int t, int c) {
    vector<int> from(n);
    vector<bool> done(n);
    queue<int> que;
    que.push(s);
    done[s] = true;
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      if (u == t) {
        vector<int> res;
        int v = u;
        while (v != s) {
          res.push_back(v);
          v = from[v];
        }
        res.push_back(s);
        reverse(res.begin(), res.end());
        return res;
      }
      rep(v, 0, n) {
        if (done[v])
          continue;
        if (c == 1) {
          if (mf.graph[u][v] == 0)
            continue;
        } else {
          if (mf.graph[u][v] != 0 or mf.graph[v][u] != 2)
            continue;
        }
        que.push(v);
        done[v] = true;
        from[v] = u;
      }
    }
    return vector<int>();
  };
  rep(i, 0, q) {
    int m, a, b;
    cin >> m >> a >> b;
    --a;
    --b;
    if (m == 1) {
      mf.add_edge(a, b, 1);
      cout << mf.calc_max_flow(0, n - 1) << endl;
    } else {
      if (mf.graph[a][b] == 1) {
        mf.graph[a][b] = mf.graph[b][a] = 0;
        cout << mf.calc_max_flow(0, n - 1) << endl;
        continue;
      }
      if (mf.graph[b][a] == 0)
        swap(a, b);
      mf.graph[a][b] = mf.graph[b][a] = 0;
      {
        vector<int> vs = search_path(a, b, 1);
        if (!vs.empty()) {
          rep(i, 0, vs.size() - 1) {
            --mf.graph[vs[i]][vs[i + 1]];
            ++mf.graph[vs[i + 1]][vs[i]];
          }
          cout << mf.calc_max_flow(0, n - 1) << endl;
          continue;
        }
      }
      --mf.flow;
      if (a != 0) {
        vector<int> vs = search_path(0, a, 0);
        rep(i, 0, vs.size() - 1) {
          ++mf.graph[vs[i]][vs[i + 1]];
          --mf.graph[vs[i + 1]][vs[i]];
        }
      }
      if (b != n - 1) {
        vector<int> vs = search_path(b, n - 1, 0);
        rep(i, 0, vs.size() - 1) {
          ++mf.graph[vs[i]][vs[i + 1]];
          --mf.graph[vs[i + 1]][vs[i]];
        }
      }
      cout << mf.calc_max_flow(0, n - 1) << endl;
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

struct max_flow {
  vector<vector<int>> graph;
  vector<bool> done;
  int size, flow;
  max_flow(int size_)
      : size(size_), graph(size_, vector<int>(size_)), done(size_), flow(0) {}
  void add_edge(int from, int to, int cap) {
    graph[from][to] = graph[to][from] = cap;
  }
  int dfs(int v, int t, int f) {
    if (v == t)
      return f;
    done[v] = true;
    for (int w = 0; w < size; ++w) {
      if (done[w] or graph[v][w] == 0)
        continue;
      int d = dfs(w, t, min(f, graph[v][w]));
      if (d > 0) {
        graph[v][w] -= d;
        graph[w][v] += d;
        return d;
      }
    }
    return 0;
  }
  int calc_max_flow(int s, int t) {
    while (true) {
      fill(done.begin(), done.end(), false);
      int f = dfs(s, t, inf);
      if (f == 0)
        return flow;
      flow += f;
    }
  }
};

void solve() {
  int n, e, q;
  cin >> n >> e >> q;
  max_flow mf(n);
  rep(i, 0, e) {
    int f, t;
    cin >> f >> t;
    --f;
    --t;
    mf.add_edge(f, t, 1);
  }
  mf.calc_max_flow(0, n - 1);
  auto search_path = [&](int s, int t, int c) {
    vector<int> from(n);
    vector<bool> done(n);
    queue<int> que;
    que.push(s);
    done[s] = true;
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      if (u == t) {
        vector<int> res;
        int v = u;
        while (v != s) {
          res.push_back(v);
          v = from[v];
        }
        res.push_back(s);
        reverse(res.begin(), res.end());
        return res;
      }
      rep(v, 0, n) {
        if (done[v])
          continue;
        if (c == 1) {
          if (mf.graph[u][v] == 0)
            continue;
        } else {
          if (mf.graph[u][v] != 0 or mf.graph[v][u] != 2)
            continue;
        }
        que.push(v);
        done[v] = true;
        from[v] = u;
      }
    }

    return vector<int>();
  };
  rep(i, 0, q) {
    int m, a, b;
    cin >> m >> a >> b;
    --a;
    --b;
    if (m == 1) {
      mf.add_edge(a, b, 1);
      cout << mf.calc_max_flow(0, n - 1) << endl;
    } else {
      int k = mf.calc_max_flow(0, n - 1);
      if (mf.graph[a][b] == 1) {
        cout << k << endl;
        continue;
      }
      if (mf.graph[b][a] == 0)
        swap(a, b);
      mf.graph[a][b] = mf.graph[b][a] = 0;
      {
        vector<int> vs = search_path(a, b, 1);
        if (!vs.empty()) {
          rep(i, 0, vs.size() - 1) {
            --mf.graph[vs[i]][vs[i + 1]];
            ++mf.graph[vs[i + 1]][vs[i]];
          }
          cout << k << endl;
          continue;
        }
      }
      --mf.flow;
      if (a != 0) {
        vector<int> vs = search_path(0, a, 0);
        rep(i, 0, vs.size() - 1) {
          ++mf.graph[vs[i]][vs[i + 1]];
          --mf.graph[vs[i + 1]][vs[i]];
        }
      }
      if (b != n - 1) {
        vector<int> vs = search_path(b, n - 1, 0);
        rep(i, 0, vs.size() - 1) {
          ++mf.graph[vs[i]][vs[i + 1]];
          --mf.graph[vs[i + 1]][vs[i]];
        }
      }
      cout << mf.calc_max_flow(0, n - 1) << endl;
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}