Problem p01430 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
/*
 */

using namespace std;

int INF = 1 << 20;
#define MAX_V 500

// ????????¨????§??????? (???????????????????????????)
struct edge {
  int to, cap, rev;
};
vector<vector<edge>> G(MAX_V); // ??°???????????£??\\???????????¨???
bool used
    [MAX_V]; // DFS??§?????§??????????????????????????°
             // from??????to??????????????????cap???????????°?????????????????????
void add_edge(int from, int to, int cap) {
  edge a;
  a.to = to;
  a.cap = cap;
  a.rev = G[to].size();
  edge b;
  b.to = from;
  b.cap = cap;
  b.rev = G[from].size();
  G[from].push_back(a);
  G[to].push_back(b);
}

void del_edge(int from, int to) {
  auto ite = G[from].begin();
  while (ite < G[from].end()) {
    if ((*ite).to == to) {
      (*ite).cap = 0;
    }
    ite++;
  }

  ite = G[to].begin();
  while (ite < G[to].end()) {
    // cout << to << " " << ( *ite ).to << " " << ( *ite ).cap << endl;
    if ((*ite).to == from) {
      (*ite).cap = 0;
    }
    ite++;
  }
}

// ?¢?????????????DFS??§??¢???
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
// s??????t???????????§???????±???????
int max_flow(int s, int t) {
  for (size_t i = 0; i < MAX_V; i++) {
    used[i] = false;
  }
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  long long int N, E, Q;
  cin >> N >> E >> Q;
  for (size_t i = 0; i < E; i++) {
    long long int F, T;
    cin >> F >> T;
    F--;
    T--;
    add_edge(F, T, 1);
    // add_edge( T, F, 1 );
  }
  for (size_t i = 0; i < Q; i++) {
    long long int M, A, B;
    cin >> M >> A >> B;
    A--;
    B--;
    if (M == 1) {
      add_edge(A, B, 1);
      // add_edge( B, A, 1 );
    } else {
      del_edge(A, B);
    }
    auto memo = G;
    cout << max_flow(0, N - 1) << endl;
    G = memo;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
/*
 */

using namespace std;

int INF = 1 << 20;
#define MAX_V 500

// ????????¨????§??????? (???????????????????????????)
struct edge {
  int to, cap, rev;
};
vector<vector<edge>> G(MAX_V); // ??°???????????£??\\???????????¨???
bool used
    [MAX_V]; // DFS??§?????§??????????????????????????°
             // from??????to??????????????????cap???????????°?????????????????????
void add_edge(int from, int to, int cap) {
  edge a;
  a.to = to;
  a.cap = cap;
  a.rev = G[to].size();
  edge b;
  b.to = from;
  b.cap = 0;
  b.rev = G[from].size();
  G[from].push_back(a);
  G[to].push_back(b);
}

void del_edge(int from, int to) {
  auto ite = G[from].begin();
  while (ite < G[from].end()) {
    if ((*ite).to == to) {
      (*ite).cap = 0;
    }
    ite++;
  }

  ite = G[to].begin();
  while (ite < G[to].end()) {
    // cout << to << " " << ( *ite ).to << " " << ( *ite ).cap << endl;
    if ((*ite).to == from) {
      (*ite).cap = 0;
    }
    ite++;
  }
}

// ?¢?????????????DFS??§??¢???
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
// s??????t???????????§???????±???????
int max_flow(int s, int t) {
  for (size_t i = 0; i < MAX_V; i++) {
    used[i] = false;
  }
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  long long int N, E, Q;
  cin >> N >> E >> Q;
  for (size_t i = 0; i < E; i++) {
    long long int F, T;
    cin >> F >> T;
    F--;
    T--;
    add_edge(F, T, 1);
    add_edge(T, F, 1);
  }
  for (size_t i = 0; i < Q; i++) {
    long long int M, A, B;
    cin >> M >> A >> B;
    A--;
    B--;
    if (M == 1) {
      add_edge(A, B, 1);
      add_edge(B, A, 1);
    } else {
      del_edge(A, B);
    }
    auto memo = G;
    cout << max_flow(0, N - 1) << endl;
    G = memo;
  }
}