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

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

const int INF = 1 << 30;

struct Dinic {
  struct edge {
    int to, cap, rev;
    bool alive;
  };

  vector<vector<edge>> graph;
  vector<int> used;

  Dinic(int n) { graph.resize(n); }

  void add_edge(int from, int to, int cap) {
    graph[from].push_back((edge){to, cap, (int)graph[to].size(), true});
    graph[to].push_back((edge){from, cap, (int)graph[from].size() - 1, true});
  }

  int dfs(int idx, const int t, int flow) {
    if (idx == t)
      return (flow);
    used[idx] = true;
    for (int i = 0; i < graph[idx].size(); i++) {
      edge &e = graph[idx][i];
      if (!e.alive)
        continue;
      if (e.cap > 0 && !used[e.to]) {
        int d = dfs(e.to, t, min(flow, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return (d);
        }
      }
    }
    return (0);
  }

  int max_flow(int s, int t, int lim) {
    int flow = 0;
    while (lim > 0) {
      used.assign(graph.size(), 0);
      int f = dfs(s, t, lim);
      if (f == 0)
        break;
      flow += f;
      lim -= f;
    }
    return (flow);
  }

  bool back_edge(int from, int to) {
    for (edge &e : graph[from]) {
      if (e.to == to && e.alive) {
        if (e.cap == 0 && max_flow(from, to, 1) == 0) {
          max_flow(from, 0, 1);
          max_flow(graph.size() - 1, to, 1);
          return (1);
        } else {
          return (0);
        }
      }
    }
  }
  void remove_edge(int from, int to) {
    for (edge &e : graph[from]) {
      if (e.to == to && e.alive) {
        e.alive = false;
        graph[e.to][e.rev].alive = false;
        return;
      }
    }
  }
};

int main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  Dinic graph(N);
  while (E--) {
    int F, T;
    cin >> F >> T;
    --F, --T;
    graph.add_edge(F, T, 1);
  }
  int flow = graph.max_flow(0, N - 1, INF);
  while (Q--) {
    int M, A, B;
    cin >> M >> A >> B;
    --A, --B;
    if (M == 1) {
      graph.add_edge(A, B, 1);
      flow += graph.max_flow(0, N - 1, 1);
    } else {
      flow -= graph.back_edge(A, B);
      flow -= graph.back_edge(B, A);
      graph.remove_edge(A, B);
    }
    cout << flow << endl;
  }
}

Error: TLE

Fixed submission:

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

const int INF = 1 << 30;

struct Dinic {
  struct edge {
    int to, cap, rev;
    bool alive;
  };

  vector<vector<edge>> graph;
  vector<int> min_cost, iter, used;
  int flow;

  Dinic(int n) { graph.resize(n); }

  void add_edge(int from, int to, int cap) {
    graph[from].push_back((edge){to, cap, (int)graph[to].size(), true});
    graph[to].push_back((edge){from, 0, (int)graph[from].size() - 1, true});
  }

  bool bfs(int s, int t) {
    min_cost.assign(graph.size(), -1);
    queue<int> que;
    min_cost[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      for (const edge &e : graph[p]) {
        if (!e.alive)
          continue;
        if (e.cap > 0 && min_cost[e.to] == -1) {
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return (min_cost[t] != -1);
  }

  int dfs(int idx, const int t, int flow) {
    if (idx == t)
      return (flow);
    for (int i = iter[idx]; i < graph[idx].size(); i++) {
      edge &e = graph[idx][i];
      if (!e.alive)
        continue;
      if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
        int d = dfs(e.to, t, min(flow, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return (d);
        }
      }
    }
    return (0);
  }
  int max_flow(int s, int t, int lim) {
    int flow = 0;
    while (flow < lim && bfs(s, t)) {
      iter.assign(graph.size(), 0);
      int f = 0;
      while ((f = dfs(s, t, lim - flow)) > 0) {
        flow += f;
      }
    }
    return (flow);
  }

  bool remove_edge(int from, int to) {
    bool flag = false;
    for (edge &e : graph[from]) {
      if (e.to == to && e.alive) {
        edge &re = graph[e.to][e.rev];
        if (e.cap == 0) {
          if (max_flow(from, to, 1) == 0) {
            max_flow(graph.size() - 1, 0, 1);
            max_flow(from, to, 1);
            flag = true;
          }
        } else if (re.cap == 0) {
          if (max_flow(to, from, 1) == 0) {
            max_flow(graph.size() - 1, 0, 1);
            max_flow(to, from, 1);
            flag = true;
          }
        }
        e.alive = re.alive = false;
        return (flag);
      }
    }
  }
};

int main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  Dinic graph(N);
  while (E--) {
    int F, T;
    cin >> F >> T;
    --F, --T;
    graph.add_edge(F, T, 1);
    graph.add_edge(T, F, 1);
  }
  int flow = graph.max_flow(0, N - 1, INF);
  while (Q--) {
    int M, A, B;
    cin >> M >> A >> B;
    --A, --B;
    if (M == 1) {
      graph.add_edge(A, B, 1);
      graph.add_edge(B, A, 1);
    } else {
      flow -= graph.remove_edge(A, B);
    }
    flow += graph.max_flow(0, N - 1, INF);
    cout << flow << endl;
  }
}