Problem p01430 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(kbrni, s) cout << " " << kbrni;                                         \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 502;
map<int, int> G[MAX_N];
bool used[MAX_N];
int n;

void add_edge(int from, int to, int cap) {
  G[from][to] = cap;
  G[to][from] = cap;
}
void erase_edge(int from, int to) {
  G[from][to] = 0;
  G[to][from] = 0;
}
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  each(it, G[v]) {
    int nx = it.fi;
    int &cap = it.se;
    if (!used[nx] && cap > 0) {
      int d = dfs(nx, t, min(f, cap));
      if (d > 0) {
        cap -= d;
        G[nx][v] += d;
        return d;
      }
    }
  }
  return 0;
}
int max_flow(int s, int t) {
  int flow = 0;
  while (true) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int E, q;
  cin >> n >> E >> q;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    add_edge(a - 1, b - 1, 1);
  }
  int res = max_flow(0, n - 1);
  rep(i, q) {
    int a, b, c;
    cin >> a >> b >> c;
    --b, --c;
    if (a == 1) {
      add_edge(b, c, 1);
      res += max_flow(0, n - 1);
    } else {
      int cap = G[b][c];
      erase_edge(b, c);
      // 逆向きに1流れてたら
      if (cap == 2) {
        swap(b, c);
      }
      if (cap != 1) {
        memset(used, 0, sizeof(used));
        int f = dfs(b, c, 1);
        // aからbにフローを1流せない(枝(a,b)を削除した分をまかなえない)
        if (f == 0) {
          memset(used, 0, sizeof(used));
          dfs(b, 0, 1);
          memset(used, 0, sizeof(used));
          dfs(n - 1, c, 1);
          --res;
        }
      }
    }
    cout << res << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \
  cout << #v << ":";                                                           \
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \
  cout << endl
#define sset(s)                                                                \
  cout << #s << ":";                                                           \
  each(kbrni, s) cout << " " << kbrni;                                         \
  cout << endl
#define smap(m)                                                                \
  cout << #m << ":";                                                           \
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 502;
map<int, int> G[MAX_N];
bool used[MAX_N];
int n;

void add_edge(int from, int to, int cap) {
  G[from][to] = cap;
  G[to][from] = cap;
}
void erase_edge(int from, int to) {
  G[from][to] = 0;
  G[to][from] = 0;
}
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  each(it, G[v]) {
    int nx = it.fi;
    int &cap = it.se;
    if (!used[nx] && cap > 0) {
      int d = dfs(nx, t, min(f, cap));
      if (d > 0) {
        cap -= d;
        G[nx][v] += d;
        return d;
      }
    }
  }
  return 0;
}
int max_flow(int s, int t) {
  int flow = 0;
  while (true) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int E, q;
  cin >> n >> E >> q;
  rep(i, E) {
    int a, b;
    cin >> a >> b;
    add_edge(a - 1, b - 1, 1);
  }
  int res = max_flow(0, n - 1);
  rep(i, q) {
    int a, b, c;
    cin >> a >> b >> c;
    --b, --c;
    if (a == 1) {
      add_edge(b, c, 1);
      res += max_flow(0, n - 1);
    } else {
      int cap = G[b][c];
      erase_edge(b, c);
      // 逆向きに1流れてたら
      if (cap == 2) {
        swap(b, c);
      }
      if (cap != 1) {
        memset(used, 0, sizeof(used));
        int f = dfs(b, c, 1);
        // aからbにフローを1流せない(枝(a,b)を削除した分をまかなえない)
        if (f == 0) {
          memset(used, 0, sizeof(used));
          dfs(b, 0, 1);
          memset(used, 0, sizeof(used));
          dfs(n - 1, c, 1);
          --res;
        }
      }
    }
    cout << res << "\n";
  }
}