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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#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 maxFlow(const vector<vector<Edge>> &edges0, int source, int sink,
            vector<vector<Edge>> &edgesOut) {
  static vector<vector<Edge>> edges;
  static vector<unsigned> index;
  static vector<int> level;
  static int n;

  class Func {
  public:
    static void bfs(int s) {
      level.assign(n, -1);
      queue<int> q;
      level[s] = 0;
      q.push(s);
      while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (unsigned i = 0; i < edges[v].size(); ++i) {
          Edge &e = edges[v][i];
          if (e.cap > 0 && level[e.to] < 0) {
            level[e.to] = level[v] + 1;
            q.push(e.to);
          }
        }
      }
    }
    static int dfs(int s, int t, int f) {
      if (s == t)
        return f;
      for (unsigned &i = index[s]; i < edges[s].size(); ++i) {
        Edge &e = edges[s][i];
        if (e.cap > 0 && level[s] < level[e.to]) {
          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;
    }
  };

  if (source == sink) {
    edgesOut = edges;
    return INT_MAX;
  }

  n = edges0.size();
  edges = edges0;

  int ret = 0;
  for (;;) {
    Func::bfs(source);
    if (level[sink] < 0) {
      edgesOut.swap(edges);
      return ret;
    }
    index.assign(n, 0);
    int f;
    while ((f = Func::dfs(source, sink, INT_MAX)) > 0)
      ret += f;
  }
}

const int INF = INT_MAX / 2;

int main() {
  int k, n, m;
  cin >> k >> n >> m;

  vector<vector<Edge>> edges(n + k + 2);
  int source = n + k + 1;
  int sink = 0;
  for (int i = 1; i <= k; ++i) {
    edges[source].push_back(Edge(i, INF, edges[i].size()));
    edges[i].push_back(Edge(source, INF, edges[source].size() - 1));
  }
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0 || b == 0) {
      if (1 <= a + b && a + b <= k) {
        cout << "overfuro" << endl;
        return 0;
      }
    }
    edges[a].push_back(Edge(b, c, edges[b].size()));
    edges[b].push_back(Edge(a, c, edges[a].size() - 1));
  }

  vector<vector<Edge>> edges2;
  int flow = maxFlow(edges, source, sink, edges2);
  vector<bool> check(n + k + 2, false);
  queue<int> q;
  check[source] = true;
  q.push(source);
  while (!q.empty()) {
    int pos = q.front();
    q.pop();
    for (const Edge &e : edges2[pos]) {
      if (e.cap > 0 && !check[e.to]) {
        check[e.to] = true;
        q.push(e.to);
      }
    }
  }

  vector<int> addFlow(n + k + 2);
  for (int i = 0; i < n + k + 2; ++i) {
    vector<vector<Edge>> edges3;
    if (check[i])
      addFlow[i] = maxFlow(edges2, source, i, edges3);
    else
      addFlow[i] = maxFlow(edges2, i, sink, edges3);
  }

  int ans = flow;
  for (int i = 0; i < n + k + 2; ++i) {
    for (const Edge &e : edges2[i]) {
      if (check[i] ^ check[e.to])
        ans = max(ans, flow + min(addFlow[i], addFlow[e.to]));
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#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 maxFlow(const vector<vector<Edge>> &edges0, int source, int sink,
            vector<vector<Edge>> &edgesOut) {
  static vector<vector<Edge>> edges;
  static vector<bool> used;

  class Func {
  public:
    static 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;
    }
  };

  edges = edges0;
  int n = edges.size();
  int ret = 0;
  for (;;) {
    used.assign(n, false);
    int f = Func::dfs(source, sink, INT_MAX);
    if (f == 0) {
      edgesOut.swap(edges);
      return ret;
    }
    ret += f;
  }
}

const int INF = INT_MAX / 2;

int main() {
  int k, n, m;
  cin >> k >> n >> m;

  vector<vector<Edge>> edges(n + k + 2);
  int source = n + k + 1;
  int sink = 0;
  for (int i = 1; i <= k; ++i) {
    edges[source].push_back(Edge(i, INF, edges[i].size()));
    edges[i].push_back(Edge(source, INF, edges[source].size() - 1));
  }
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0 || b == 0) {
      if (1 <= a + b && a + b <= k) {
        cout << "overfuro" << endl;
        return 0;
      }
    }
    edges[a].push_back(Edge(b, c, edges[b].size()));
    edges[b].push_back(Edge(a, c, edges[a].size() - 1));
  }

  vector<vector<Edge>> edges2;
  int flow = maxFlow(edges, source, sink, edges2);
  vector<bool> check(n + k + 2, false);
  queue<int> q;
  check[source] = true;
  q.push(source);
  while (!q.empty()) {
    int pos = q.front();
    q.pop();
    for (const Edge &e : edges2[pos]) {
      if (e.cap > 0 && !check[e.to]) {
        check[e.to] = true;
        q.push(e.to);
      }
    }
  }

  vector<int> addFlow(n + k + 2);
  for (int i = 0; i < n + k + 2; ++i) {
    vector<vector<Edge>> edges3;
    if (i == source || i == sink)
      addFlow[i] = INF;
    if (check[i])
      addFlow[i] = maxFlow(edges2, source, i, edges3);
    else
      addFlow[i] = maxFlow(edges2, i, sink, edges3);
  }

  int ans = flow;
  for (int i = 0; i < n + k + 2; ++i) {
    for (const Edge &e : edges2[i]) {
      if (check[i] ^ check[e.to])
        ans = max(ans, flow + min(addFlow[i], addFlow[e.to]));
    }
  }
  cout << ans << endl;

  return 0;
}