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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using ti3 = tuple<int, int, int>;
using vti3 = vector<ti3>;
template <class T, int s> using va = vector<array<T, s>>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define rrep(i, a) for (int i = (a)-1; i >= 0; i--)
#define rrep1(i, a) for (int i = a; i; i--)
#define repi(i, a, b)                                                          \\
  for (int i = a; i < b; i++)                                                  \\
    ;
const ll mod = 1000000007;
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>;
template <class T> inline void hash_combine(size_t &seed, const T &v) {
  hash<T> hasher;
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
namespace std {
template <typename S, typename T> struct hash<pair<S, T>> {
  inline size_t operator()(const pair<S, T> &v) const {
    size_t seed = 0;
    hash_combine(seed, v.first);
    hash_combine(seed, v.second);
    return seed;
  }
};
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
  static void apply(size_t &seed, Tuple const &tuple) {
    HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
    hash_combine(seed, std::get<Index>(tuple));
  }
};
template <class Tuple> struct HashValueImpl<Tuple, 0> {
  static void apply(size_t &seed, Tuple const &tuple) {
    hash_combine(seed, std::get<0>(tuple));
  }
};
template <typename... TT> struct hash<std::tuple<TT...>> {
  size_t operator()(std::tuple<TT...> const &tt) const {
    size_t seed = 0;
    HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
    return seed;
  }
};
} // namespace std
template <class T> int id(vector<T> &a, T b) {
  return lower_bound(ALL(a), b) - a.begin();
}
ll pow(ll base, ll i, ll mod) {
  ll a = 1;
  while (i) {
    if (i & 1) {
      a *= base;
      a %= mod;
    }
    base *= base;
    base %= mod;
    i /= 2;
  }
  return a;
}
ll gcd(ll a, ll b) {
  while (b) {
    ll c = a % b;
    a = b;
    b = c;
  }
  return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int popcnt(unsigned long long a) {
  a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555);
  a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333);
  a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f);
  a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff);
  a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff);
  return (a & 0xffffffff) + (a >> 32);
}
class Flow {
  int V;
  struct edge {
    int to, cap, rev, cost, add;
  };
  vector<vector<edge>> G;
  vector<int> level, iter, h, dist, prevv, preve;
  void bfs(int s) {
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < 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 < 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 res = 0;
  int flow = 0;
  int s, t;
  int max_flow(int s, int t, int fmax = numeric_limits<int>::max()) {
    level.resize(V);
    iter.resize(V);
    for (;;) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      fill(iter.begin(), iter.end(), 0);
      int f;
      while ((f = dfs(s, t, fmax)) > 0) {
        flow += f;
        fmax -= f;
      }
      if (fmax == 0)
        return flow;
    }
  }
  typedef pair<int, int> P;
  int min_cost_flow(int s, int t, int f) {
    h.resize(V);
    dist.resize(V);
    prevv.resize(V);
    preve.resize(V);
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> que;
      fill(dist.begin(), dist.end(), numeric_limits<int>::max());
      dist[s] = 0;
      que.push({0, s});
      while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (dist[v] < p.first)
          continue;
        for (int i = 0; i < G[v].size(); i++) {
          edge &e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
            dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            que.push({dist[e.to], e.to});
          }
        }
      }
      if (dist[t] == numeric_limits<int>::max()) {
        return -1;
      }
      for (int v = 0; v < V; v++)
        h[v] += dist[v];

      int d = f;
      for (int v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * h[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = G[prevv[v]][preve[v]];
        e.cap -= d;
        G[v][e.rev].cap += d;
      }
    }
    return res;
  }

public:
  Flow(int size, int s, int t) : G(size + 1), V(size + 1), s(s), t(t) {}
  void add_edge(int from, int to, int cap, int cost = 0) {
    G[from].push_back(edge{to, cap, (int)G[to].size(), cost, 1});
    G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost, 0});
  }
  void remove_edge_max(int from, int to, int cap, int cost = 0) {
    for (auto &x : G[from]) {
      if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap ||
          x.cost != cost)
        continue;
      int prev = flow;
      int cap = G[to][x.rev].cap;
      G[to][x.rev].cap = 0;
      x.cap = 0;
      cap -= max_flow(from, to, cap) - prev;
      max_flow(t, to, cap);
      max_flow(from, s, cap);
      flow = prev - cap;
      break;
    }
  }
  void remove_edge_cost(int from, int to, int cap, int cost = 0) {
    for (auto &x : G[from]) {
      if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap ||
          x.cost != cost)
        continue;
      cost += G[to][x.rev].cap * G[to][x.rev].cost;
      int cap = G[to][x.rev].cap;
      G[to][x.rev].cap = 0;
      x.cap = 0;
      min_cost_flow(from, to, cap);
      break;
    }
  }
  int max_flow() { return max_flow(s, t); }
  int min_cost_flow(int f) { return min_cost_flow(s, t, f); }
};
// end of lib
// template<class S=void,int ptr_num, class T = char>class trie {
//	umap<T, trie<S, ptr_num, T> next;
// public:
//	S key;
//	trie<S, ptr_num, T>* ptr[ptr_num] = {};
//	trie(S &&data) :key(data) {}
//	trie(const S &data) :key(data) {}
//	void add(T x,S data) {
//		if (!next.find(x))next.insert(x, data);
//	}
//	trie& operator[](T x) {
//		return next[x];
//	}
//	bool find(T x) {
//		retun next.find(x);
//	}
// };
// template<class T=char>class AhoCorasick {
//	trie<pair<bool,int>, 2, T> tree;
//	AhoCorasick(vector<string> p) {
//		int num = 0;
//		vector<decltype(&tree)> que(p.size(),&tree);
//		for (int i = 0;; i++) {
//			bool end = 1;
//			int i = 0;
//			for (auto a : p) {
//				if (i >= a.size())break;
//				end = ;0
//				que[i] = (*que[i])[a[i]];
//				i++;
//			}
//			if (end)break;
//		}
//	}
// };
int main() {
  int n, e, q;
  cin >> n >> e >> q;
  Flow flow(n, 1, n);
  rep(i, e) {
    int f, t;
    cin >> f >> t;
    flow.add_edge(f, t, 1);
    flow.add_edge(t, f, 1);
  }
  int offset = 0;
  rep(i, q) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 1) {
      flow.add_edge(a, b, 1);
      flow.add_edge(b, a, 1);
    } else {
      flow.remove_edge_max(a, b, 1);
      flow.remove_edge_max(b, a, 1);
    }
    cout << flow.max_flow() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// cin.sync_with_stdio(false);
// streambuf
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
using vi = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using ti3 = tuple<int, int, int>;
using vti3 = vector<ti3>;
template <class T, int s> using va = vector<array<T, s>>;
template <class T, class T2> using umap = unordered_map<T, T2>;
template <class T> using uset = unordered_set<T>;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define ALL(a) a.begin(), a.end()
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define rrep(i, a) for (int i = (a)-1; i >= 0; i--)
#define rrep1(i, a) for (int i = a; i; i--)
#define repi(i, a, b)                                                          \\
  for (int i = a; i < b; i++)                                                  \\
    ;
const ll mod = 1000000007;
template <class T> using heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>;
template <class T> inline void hash_combine(size_t &seed, const T &v) {
  hash<T> hasher;
  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
namespace std {
template <typename S, typename T> struct hash<pair<S, T>> {
  inline size_t operator()(const pair<S, T> &v) const {
    size_t seed = 0;
    hash_combine(seed, v.first);
    hash_combine(seed, v.second);
    return seed;
  }
};
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
  static void apply(size_t &seed, Tuple const &tuple) {
    HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
    hash_combine(seed, std::get<Index>(tuple));
  }
};
template <class Tuple> struct HashValueImpl<Tuple, 0> {
  static void apply(size_t &seed, Tuple const &tuple) {
    hash_combine(seed, std::get<0>(tuple));
  }
};
template <typename... TT> struct hash<std::tuple<TT...>> {
  size_t operator()(std::tuple<TT...> const &tt) const {
    size_t seed = 0;
    HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
    return seed;
  }
};
} // namespace std
template <class T> int id(vector<T> &a, T b) {
  return lower_bound(ALL(a), b) - a.begin();
}
ll pow(ll base, ll i, ll mod) {
  ll a = 1;
  while (i) {
    if (i & 1) {
      a *= base;
      a %= mod;
    }
    base *= base;
    base %= mod;
    i /= 2;
  }
  return a;
}
ll gcd(ll a, ll b) {
  while (b) {
    ll c = a % b;
    a = b;
    b = c;
  }
  return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int popcnt(unsigned long long a) {
  a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555);
  a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333);
  a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f);
  a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff);
  a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff);
  return (a & 0xffffffff) + (a >> 32);
}
class Flow {
  int V;
  struct edge {
    int to, cap, rev, cost;
  };
  vector<vector<edge>> G;
  vector<int> level, iter, h, dist, prevv, preve;
  void bfs(int s) {
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < 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 < 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 res = 0;
  int flow = 0;
  int s, t;
  int max_flow(int s, int t, int fmax = numeric_limits<int>::max()) {
    level.resize(V);
    iter.resize(V);
    if (s == t)
      return fmax;
    for (;;) {
      bfs(s);
      if (level[t] < 0)
        return flow;
      fill(iter.begin(), iter.end(), 0);
      int f;
      while ((f = dfs(s, t, fmax)) > 0) {
        flow += f;
        fmax -= f;
      }
    }
  }
  typedef pair<int, int> P;
  int min_cost_flow(int s, int t, int f) {
    h.resize(V);
    dist.resize(V);
    prevv.resize(V);
    preve.resize(V);
    fill(h.begin(), h.end(), 0);
    while (f > 0) {
      priority_queue<P, vector<P>, greater<P>> que;
      fill(dist.begin(), dist.end(), numeric_limits<int>::max());
      dist[s] = 0;
      que.push({0, s});
      while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (dist[v] < p.first)
          continue;
        for (int i = 0; i < G[v].size(); i++) {
          edge &e = G[v][i];
          if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
            dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            que.push({dist[e.to], e.to});
          }
        }
      }
      if (dist[t] == numeric_limits<int>::max()) {
        return -1;
      }
      for (int v = 0; v < V; v++)
        h[v] += dist[v];

      int d = f;
      for (int v = t; v != s; v = prevv[v]) {
        d = min(d, G[prevv[v]][preve[v]].cap);
      }
      f -= d;
      res += d * h[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = G[prevv[v]][preve[v]];
        e.cap -= d;
        G[v][e.rev].cap += d;
      }
    }
    return res;
  }

public:
  Flow(int size, int s, int t) : G(size + 1), V(size + 1), s(s), t(t) {}
  void add_edge(int from, int to, int cap, int cost = 0) {
    G[from].push_back(edge{to, cap, (int)G[to].size(), cost});
    G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost});
  }
  void remove_edge_max(int from, int to, int cap, int cost = 0) {
    for (auto &x : G[from]) {
      if (x.cap + G[to][x.rev].cap != cap || x.to != to || x.cost != cost)
        continue;
      int prev = flow - G[to][x.rev].cap;
      max_flow(t, to, G[to][x.rev].cap);
      max_flow(from, s, G[to][x.rev].cap);
      G[to][x.rev].cap = 0;
      x.cap = 0;
      break;
    }
  }
  void remove_edge_cost(int from, int to, int cap, int cost = 0) {
    for (auto &x : G[from]) {
      if (x.cap + G[to][x.rev].cap != cap || x.to != to || x.cost != cost)
        continue;
      int prev = flow - G[to][x.rev].cap;
      min_cost_flow(t, to, G[to][x.rev].cap);
      min_cost_flow(from, s, G[to][x.rev].cap);
      G[to][x.rev].cap = 0;
      x.cap = 0;
      break;
    }
  }
  int max_flow() { return max_flow(s, t); }
  int min_cost_flow(int f) { return min_cost_flow(s, t, f); }
};
// end of lib
// template<class S=void,int ptr_num, class T = char>class trie {
//	umap<T, trie<S, ptr_num, T> next;
// public:
//	S key;
//	trie<S, ptr_num, T>* ptr[ptr_num] = {};
//	trie(S &&data) :key(data) {}
//	trie(const S &data) :key(data) {}
//	void add(T x,S data) {
//		if (!next.find(x))next.insert(x, data);
//	}
//	trie& operator[](T x) {
//		return next[x];
//	}
//	bool find(T x) {
//		retun next.find(x);
//	}
// };
// template<class T=char>class AhoCorasick {
//	trie<pair<bool,int>, 2, T> tree;
//	AhoCorasick(vector<string> p) {
//		int num = 0;
//		vector<decltype(&tree)> que(p.size(),&tree);
//		for (int i = 0;; i++) {
//			bool end = 1;
//			int i = 0;
//			for (auto a : p) {
//				if (i >= a.size())break;
//				end = ;0
//				que[i] = (*que[i])[a[i]];
//				i++;
//			}
//			if (end)break;
//		}
//	}
// };
int main() {
  int n, e, q;
  cin >> n >> e >> q;
  Flow flow(n, 1, n);
  rep(i, e) {
    int f, t;
    cin >> f >> t;
    flow.add_edge(f, t, 1);
  }
  int offset = 0;
  rep(i, q) {
    int m, a, b;
    cin >> m >> a >> b;
    if (m == 1) {
      flow.add_edge(a, b, 1);
      flow.add_edge(b, a, 1);
    } else {
      flow.remove_edge_max(a, b, 1);
      flow.remove_edge_max(b, a, 1);
    }
    cout << flow.max_flow() << endl;
  }
}