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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cap, rev;
  Edge(){};
  Edge(int to0, int cap0) {
    to = to0;
    cap = cap0;
  }
  Edge(int to0, int cap0, int rev0) {
    to = to0;
    cap = cap0;
    rev = rev0;
  }
};

int n;
vector<vector<Edge>> edges;
vector<bool> used;

int dfs(int s, int t, int f) {
  if (s == t)
    return f;
  used[s] = true;
  for (unsigned i = 0; i < edges[s].size(); ++i) {
    Edge &e = edges[s][i];
    if (!used[e.to] && e.cap > 0) {
      int g = dfs(e.to, t, min(f, e.cap));
      if (g > 0) {
        e.cap -= g;
        edges[e.to][e.rev].cap += g;
        return g;
      }
    }
  }
  return 0;
}

int flow(int s, int t, int f) {
  used.assign(n + 1, false);
  return dfs(s, t, f);
}

int main() {
  int e, q;
  cin >> n >> e >> q;

  edges.assign(n + 1, vector<Edge>());
  for (int i = 0; i < e; ++i) {
    int f, t;
    cin >> f >> t;
    edges[f].push_back(Edge(t, 1, edges[t].size()));
    edges[t].push_back(Edge(f, 1, edges[f].size() - 1));
  }

  int ret = 0;
  for (;;) {
    int f = flow(1, n, INT_MAX);
    if (f == 0)
      break;
    ret += f;
  }

  for (int i = 0; i < q; ++i) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 1) {
      unsigned j = 0;
      while (j < edges[a].size() && edges[a][j].to != b)
        ++j;
      if (j < edges[a].size()) {
        edges[a][j].cap = 1;
        edges[b][edges[a][j].rev].cap = 1;
      } else {
        edges[a].push_back(Edge(b, 1, edges[b].size()));
        edges[b].push_back(Edge(a, 1, edges[a].size() - 1));
      }
      ret += flow(1, n, 1);
    } else {
      unsigned j = 0;
      while (edges[a][j].to != b)
        ++j;
      if (edges[a][j].cap == 1) {
        edges[a][j].cap = 0;
        edges[b][edges[a][j].rev].cap = 0;
      } else {
        if (edges[a][j].cap != 0) {
          j = edges[a][j].rev;
          swap(a, b);
        }
        edges[b][edges[a][j].rev].cap = 0;
        if (flow(a, b, 1) == 0) {
          flow(a, 1, 1);
          flow(n, b, 1);
          --ret;
          ret += flow(1, n, 1);
        }
      }
    }
    cout << ret << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cap, rev;
  Edge(){};
  Edge(int to0, int cap0) {
    to = to0;
    cap = cap0;
  }
  Edge(int to0, int cap0, int rev0) {
    to = to0;
    cap = cap0;
    rev = rev0;
  }
};

int n;
vector<vector<Edge>> edges;
vector<bool> used;

int dfs(int s, int t, int f) {
  if (s == t)
    return f;
  used[s] = true;
  for (unsigned i = 0; i < edges[s].size(); ++i) {
    Edge &e = edges[s][i];
    if (!used[e.to] && e.cap > 0) {
      int g = dfs(e.to, t, min(f, e.cap));
      if (g > 0) {
        e.cap -= g;
        edges[e.to][e.rev].cap += g;
        return g;
      }
    }
  }
  return 0;
}

int flow(int s, int t) {
  used.assign(n + 1, false);
  return dfs(s, t, INT_MAX);
}

bool dfs2(int s, int t) {
  if (s == t)
    return true;
  used[s] = true;
  for (unsigned i = 0; i < edges[s].size(); ++i) {
    Edge &e = edges[s][i];
    if (!used[e.to] && e.cap > 1 && dfs2(e.to, t)) {
      --e.cap;
      ++edges[e.to][e.rev].cap;
      return true;
    }
  }
  return false;
}

void flowBack(int s, int t) {
  used.assign(n + 1, false);
  if (!dfs2(s, t))
    for (;;)
      ;
}

int main() {
  int e, q;
  cin >> n >> e >> q;

  edges.assign(n + 1, vector<Edge>());
  for (int i = 0; i < e; ++i) {
    int f, t;
    cin >> f >> t;
    edges[f].push_back(Edge(t, 1, edges[t].size()));
    edges[t].push_back(Edge(f, 1, edges[f].size() - 1));
  }

  int ret = 0;
  for (;;) {
    int f = flow(1, n);
    if (f == 0)
      break;
    ret += f;
  }

  for (int i = 0; i < q; ++i) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 1) {
      unsigned j = 0;
      while (j < edges[a].size() && edges[a][j].to != b)
        ++j;
      if (j < edges[a].size()) {
        edges[a][j].cap = 1;
        edges[b][edges[a][j].rev].cap = 1;
      } else {
        edges[a].push_back(Edge(b, 1, edges[b].size()));
        edges[b].push_back(Edge(a, 1, edges[a].size() - 1));
      }
      ret += flow(1, n);
    } else {
      unsigned j = 0;
      while (edges[a][j].to != b)
        ++j;
      if (edges[a][j].cap == 1) {
        edges[a][j].cap = 0;
        edges[b][edges[a][j].rev].cap = 0;
      } else {
        if (edges[a][j].cap == 0) {
          edges[b][edges[a][j].rev].cap = 0;
          flowBack(a, 1);
          flowBack(n, b);
        } else {
          edges[a][j].cap = 0;
          flowBack(b, 1);
          flowBack(n, a);
        }
        --ret;
        ret += flow(1, n);
      }
    }
    cout << ret << endl;
  }

  return 0;
}