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

#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 28
#define MAX_V 600
struct edge {
  int to, cap, rev;
  edge() {}
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

vector<edge> G[MAX_V], G2[MAX_V];
map<int, int> M[MAX_V];
int level[MAX_V];
int iter[MAX_V];

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

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < (int)G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < (int)G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      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;
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  for (int i = 0, f, t; i < E; i++)
    scanf("%d%d", &f, &t), add_edge(f, t, 1);
  int prev = 0;
  for (int i = 1; i <= N; i++)
    G[i] = G2[i];
  while (Q--) {
    int m, a, b;
    scanf("%d%d%d", &m, &a, &b);
    if (m == 1) {

      if (M[a].find(b) == M[a].end()) {
        add_edge(a, b, 1);
        G[a].push_back(edge(b, 1, G[b].size()));
        G[b].push_back(edge(a, 1, G[a].size() - 1));
      } else {
        G[a][M[a][b]].cap = G[b][M[b][a]].cap = 1;
        G2[a][M[a][b]].cap = G2[b][M[b][a]].cap = 1;
      }
    } else {
      G2[a][M[a][b]].cap = G2[b][M[b][a]].cap = 0;
      for (int i = 1; i <= N; i++)
        G[i] = G2[i];
      prev = 0;
    }
    prev = prev + max_flow(1, N);
    printf("%d\\n", prev);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1 << 28
#define MAX_V 600
struct edge {
  int to, cap, rev;
  edge() {}
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

vector<edge> G[MAX_V], G2[MAX_V];
map<int, int> M[MAX_V];
int level[MAX_V];
int iter[MAX_V];

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

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < (int)G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < (int)G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      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;
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  int N, E, Q;
  cin >> N >> E >> Q;
  for (int i = 0, f, t; i < E; i++)
    scanf("%d%d", &f, &t), add_edge(f, t, 1);
  int prev = 0;
  for (int i = 1; i <= N; i++)
    G[i] = G2[i];
  while (Q--) {
    int m, a, b;
    scanf("%d%d%d", &m, &a, &b);
    if (m == 1) {

      if (M[a].find(b) == M[a].end()) {
        add_edge(a, b, 1);
        // G[a].push_back(edge(b,1,G[a].size()));
        // G[b].push_back(edge(a,1,G[b].size()-1));
      } else {
        // G[a][M[a][b]].cap=G[b][M[b][a]].cap=1;
        G2[a][M[a][b]].cap = G2[b][M[b][a]].cap = 1;
      }
      for (int i = 1; i <= N; i++)
        G[i] = G2[i];
      prev = 0;
    } else {
      G2[a][M[a][b]].cap = G2[b][M[b][a]].cap = 0;
      for (int i = 1; i <= N; i++)
        G[i] = G2[i];
      prev = 0;
    }
    prev = prev + max_flow(1, N);
    printf("%d\\n", prev);
  }
  return 0;
}