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

#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = std::vector<std::vector<T>>;
template <typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template <class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template <class T> inline void print(T x) { std::cout << x << std::endl; }
template <class T> inline bool inside(T y, T x, T H, T W) {
  return 0 <= y and y < H and 0 <= x and x < W;
}
inline double distance(double y1, double x1, double y2, double x2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = {0, 1, 0, -1},
                       dx = {1, 0, -1, 0}; // 4近傍（右, 下, 左, 上）

using namespace std;

struct Edge {
  const int to;
  const int rev;
  int flow;
  const int capacity;
  const bool is_rev;
  Edge(int to, int rev, int flow, int capacity, bool is_rev)
      : to(to), rev(rev), flow(flow), capacity(capacity), is_rev(is_rev) {}
};

struct Vertex {
  int h;
  int e_flow;
  Vertex(int h, int e_flow) : h(h), e_flow(e_flow) {}
};

class Graph {
public:
  const int V; // No. of vertices
  vector<Vertex> ver;
  vector<vector<Edge>> graph;

  Graph(int V) : V(V) {
    for (int i = 0; i < V; i++) {
      ver.emplace_back(Vertex(0, 0));
    }
    graph.resize(V);
  }

  void addEdge(int from, int to, int capacity) {
    graph[from].emplace_back(Edge(to, graph[to].size(), 0, capacity, false));
    graph[to].emplace_back(
        Edge(from, graph[from].size() - 1, capacity, capacity, true));
  }

  void preflow(int s) {
    ver[s].h = (int)ver.size();

    for (Edge &edge : graph[s]) {
      if (edge.is_rev) {
        continue;
      }
      edge.flow = edge.capacity;
      ver[edge.to].e_flow += edge.flow;
      graph[edge.to][edge.rev].flow = 0;
      //            graph[edge.to].emplace_back(Edge(s, -1, edge.capacity -
      //            edge.flow, edge.capacity)); assert(edge.capacity - edge.flow
      //            == 0);
    }
  }

  int overFlowVertex(const vector<Vertex> &ver) {
    for (int i = 1; i < ver.size() - 1; i++) {
      if (ver[i].e_flow > 0) {
        return i;
      }
    }
    return -1;
  }

  void updateReverseEdgeFlow(int from, int to, int flow) {

    for (Edge &edge : graph[to]) {
      if (edge.to == from) {
        return;
      }
    }
    assert(false);
    auto e = graph[from][to];
    graph[to].emplace_back(Edge(from, -1, e.capacity, e.capacity, true));
  }

  // fromから他のノードにpushする
  bool push(int from) {
    for (Edge &edge : graph[from]) {
      if (edge.flow == edge.capacity) {
        continue;
      }

      if (ver[from].h > ver[edge.to].h) {
        int flow = min(edge.capacity - edge.flow, ver[from].e_flow);
        ver[from].e_flow -= flow;
        ver[edge.to].e_flow += flow;
        edge.flow += flow;

        //                updateReverseEdgeFlow(from, edge.to, flow);
        graph[edge.to][edge.rev].flow -= flow;
        return true;
      }
    }
    return false;
  }

  void relabel(int u) {
    int mh = INT_MAX;
    for (const Edge &edge : graph[u]) {
      if (edge.flow == edge.capacity) {
        continue;
      }

      if (ver[edge.to].h < mh) {
        mh = ver[edge.to].h;
        ver[u].h = mh + 1;
      }
    }
  }

  int maxFlow(int s, int t) {
    preflow(s);
    while (overFlowVertex(ver) != -1) {
      int u = overFlowVertex(ver);
      if (!push(u))
        relabel(u);
    }
    return ver.back().e_flow;
  }
};

int main() {
  int V, E;
  cin >> V >> E;
  Graph ff(V);
  for (int i = 0; i < E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    ff.addEdge(u, v, c);
  }

  cout << ff.maxFlow(0, V - 1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = (a); i < ((int)b); ++i)
#define RFOR(i, a) for (int i = (a); i >= 0; --i)
#define FOE(i, a) for (auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x) cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template <typename T> using V = std::vector<T>;
template <typename T> using VV = std::vector<std::vector<T>>;
template <typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template <class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template <class T> inline void print(T x) { std::cout << x << std::endl; }
template <class T> inline bool inside(T y, T x, T H, T W) {
  return 0 <= y and y < H and 0 <= x and x < W;
}
inline double distance(double y1, double x1, double y2, double x2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = {0, 1, 0, -1},
                       dx = {1, 0, -1, 0}; // 4近傍（右, 下, 左, 上）

using namespace std;

struct Edge {
  const int to;
  int flow;
  const int capacity;
  Edge(int to, int flow, int capacity)
      : to(to), flow(flow), capacity(capacity) {}
};

struct Vertex {
  int h;
  int e_flow;
  Vertex(int h, int e_flow) : h(h), e_flow(e_flow) {}
};

class Graph {
public:
  const int V; // No. of vertices
  vector<Vertex> ver;
  vector<vector<Edge>> graph;

  Graph(int V) : V(V) {
    for (int i = 0; i < V; i++) {
      ver.emplace_back(Vertex(0, 0));
    }
    graph.resize(V);
  }

  void addEdge(int from, int to, int capacity) {
    graph[from].emplace_back(Edge(to, 0, capacity));
    graph[to].emplace_back(Edge(from, capacity, capacity));
  }

  void preflow(int s) {
    ver[s].h = (int)ver.size();

    for (Edge &edge : graph[s]) {
      edge.flow = edge.capacity;
      ver[edge.to].e_flow += edge.flow;
      //            graph[edge.to].emplace_back(Edge(s, edge.capacity -
      //            edge.flow, edge.capacity));
    }
  }

  int overFlowVertex(const vector<Vertex> &ver) {
    for (int i = 1; i < ver.size() - 1; i++) {
      if (ver[i].e_flow > 0) {
        return i;
      }
    }
    return -1;
  }

  void updateReverseEdgeFlow(int from, int to, int flow) {
    for (Edge &edge : graph[to]) {
      if (edge.to == from) {
        edge.flow -= flow;
        return;
      }
    }
    // graph[to].emplace_back(Edge(from, 0, flow));
  }

  // fromから他のノードにpushする
  bool push(int from) {
    for (Edge &edge : graph[from]) {
      if (edge.flow == edge.capacity) {
        continue;
      }

      if (ver[from].h > ver[edge.to].h) {
        int flow = min(edge.capacity - edge.flow, ver[from].e_flow);
        ver[from].e_flow -= flow;
        ver[edge.to].e_flow += flow;
        edge.flow += flow;
        updateReverseEdgeFlow(from, edge.to, flow);
        return true;
      }
    }
    return false;
  }

  void relabel(int u) {
    int mh = INT_MAX;
    for (const Edge &edge : graph[u]) {
      if (edge.flow == edge.capacity) {
        continue;
      }

      if (ver[edge.to].h < mh) {
        mh = ver[edge.to].h;
        ver[u].h = mh + 1;
      }
    }
  }

  int maxFlow(int s, int t) {
    preflow(s);
    while (overFlowVertex(ver) != -1) {
      int u = overFlowVertex(ver);
      if (!push(u))
        relabel(u);
    }
    return ver.back().e_flow;
  }
};

int main() {
  int V, E;
  cin >> V >> E;
  Graph ff(V);
  for (int i = 0; i < E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    ff.addEdge(u, v, c);
  }

  cout << ff.maxFlow(0, V - 1) << endl;
  return 0;
}