alexjercan/bugnet huggingface dataset:
Problem p02373 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using P = pair<int, int>;
const int INF = 1 << 29;
const int MAX_V = 100010;
vector<int> children[MAX_V];

int dep[MAX_V];
int par[MAX_V][20];
void dfs(int node, int p, int d) {
  dep[node] = d;
  for (int child : children[node]) {
    if (child == p)
      continue;
    par[child][0] = node;
    dfs(child, node, d + 1);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b])
    swap(a, b);
  REP(i, 20) if ((dep[a] - dep[b]) & (1 << i)) a = par[a][i];
  if (a == b)
    return a;
  for (int i = 19; i >= 0; i--)
    if (par[a][i] != par[b][i])
      a = par[a][i], b = par[b][i];
  return par[a][0];
}

int main() {
  _;
  int n, q, k, u, v;
  cin >> n;
  REP(i, n) {
    cin >> k;
    children[i] = vector<int>(k);
    REP(j, k) cin >> children[i][j];
  }
  dfs(0, -1, 0);
  REP(i, 19) REP(j, n) par[j][i + 1] = par[par[j][i]][i];
  cin >> q;
  REP(i, q) {
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using P = pair<int, int>;
const int INF = 1 << 29;
const int MAX_V = 10010;
vector<int> children[MAX_V];

int dep[100010];
int par[100010][20];
void dfs(int node, int p, int d) {
  dep[node] = d;
  for (int child : children[node]) {
    if (child == p)
      continue;
    par[child][0] = node;
    dfs(child, node, d + 1);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b])
    swap(a, b);
  REP(i, 20) if ((dep[a] - dep[b]) & (1 << i)) a = par[a][i];
  if (a == b)
    return a;
  for (int i = 19; i >= 0; i--)
    if (par[a][i] != par[b][i])
      a = par[a][i], b = par[b][i];
  return par[a][0];
}

int main() {
  _;
  int n, q, k, u, v;
  cin >> n;
  REP(i, n) {
    cin >> k;
    children[i] = vector<int>(k);
    REP(j, k) cin >> children[i][j];
  }
  dfs(0, -1, 0);
  REP(i, 19) REP(j, n) par[j][i + 1] = par[par[j][i]][i];
  cin >> q;
  REP(i, q) {
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
}
Problem p02373 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i : a)
#define ITR(x, a) for (__typeof(a.begin()) x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define endl '\\n'

int root = 0;
int n;
vector<int> c[100005];
const int K = 20;
int parent[K][100005];
int depth[100005];

void bfs(int v, int p, int d) {
  queue<tuple<int, int, int>> que;
  que.push(make_tuple(v, p, d));
  while (!que.empty()) {
    auto ele = que.front();
    que.pop();
    int vv = get<0>(ele), pp = get<1>(ele), dd = get<2>(ele);
    parent[0][vv] = pp;
    depth[vv] = dd;
    for (auto i : c[vv]) {
      que.push(make_tuple(i, vv, dd + 1));
    }
  }
}

void init() {
  bfs(root, -1, 0);
  for (int k = 0; k + 1 < K; k++) {
    for (int v = 0; v < n; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k; k < K; k++) {
    if ((depth[v] - depth[u] >> k & 1)) {
      v = parent[k][v];
    }
    if (u == v)
      return u;
  }
  for (int k = K - 1; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int cj;
      cin >> cj;
      c[i].push_back(cj);
    }
  }

  init();

  int q;
  cin >> q;
  rep(i, q) {
    int u, v;
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T> using Graph = vector<vector<T>>;

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i : a)
#define ITR(x, a) for (__typeof(a.begin()) x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define endl '\\n'

int n;
vector<int> c[100005];
int p[100005];
int d[100005];

void dfs(int k, int depth) {
  d[k] = depth;
  for (auto i : c[k]) {
    dfs(i, depth + 1);
  }
}

int lca(int l, int r) {
  while (l != r) {
    if (d[l] > d[r])
      l = p[l];
    else
      r = p[r];
  }
  return l;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  rep(i, n) p[i] = -1;
  rep(i, n) {
    int k;
    cin >> k;
    rep(j, k) {
      int cj;
      cin >> cj;
      c[i].push_back(cj);
      p[cj] = i;
    }
  }

  int root = 0;
  rep(i, n) if (p[i] == -1) root = i;

  dfs(root, 0);

  int q;
  cin >> q;
  rep(i, q) {
    int u, v;
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }

  return 0;
}
Problem p02373 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

const int MAXV = 100100;
vector<int> children[MAXV];
int parent[30][MAXV];
int depth[MAXV];

int n;

void dfs(int v, int d) {
  depth[v] = d;
  for (int i = 0; i < children[v].size(); i++) {
    dfs(children[v][i], d + 1);
  }
}

void init(int V) {
  dfs(0, 0);
  for (int k = 0; k < 30; k++) {
    for (int v = 0; v < V; v++) {
      if (parent[k][v] < 0)
        parent[k + 1][v] = -1;
      else
        parent[k + 1][v] = parent[k][parent[k][v]];
    }
  }
}

int lca(int u, int v) {
  if (depth[u] > depth[v])
    swap(u, v);
  for (int k = 0; k < 30; k++) {
    if ((depth[v] - depth[u]) >> k & 1) {
      v = parent[k][v];
    }
  }
  if (u == v)
    return u;
  for (int k = 29; k >= 0; k--) {
    if (parent[k][u] != parent[k][v]) {
      u = parent[k][u];
      v = parent[k][v];
    }
  }
  return parent[0][u];
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      children[i].push_back(c);
      parent[0][c] = i;
    }
  }
  init(n);
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    cout << lca(u, v) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n;
int main(void) {
  cin >> n;
  int parent[n];
  for (int i = 0; i < n; i++)
    parent[i] = -1;
  for (int i = 0; i < n; i++) {
    int k, tmp;
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> tmp;
      parent[tmp] = i;
    }
  }
  //    for (int i = 0; i < n; i++) {
  //        printf("%d parent is %d\\n", i, parent[i]);
  //    }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, v;
    cin >> u >> v;
    vector<int> parents_u, parents_v;
    for (int j = u; j != -1; j = parent[j]) {
      parents_u.push_back(j);
    }
    for (int j = v; j != -1; j = parent[j]) {
      parents_v.push_back(j);
    }
    for (int j = 0; j < parents_u.size(); j++) {
      int flag = 0;
      for (int k = 0; k < parents_v.size(); k++) {
        if (parents_u[j] == parents_v[k]) {
          cout << parents_u[j] << endl;
          flag = 1;
          break;
        }
      }
      if (flag == 1)
        break;
    }
  }
  return 0;
}
Problem p02373 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

const int MAX = 1e5;
pii table[18][MAX * 2 - 1];

int n;
vector<int> G[MAX];
int id[MAX];

int it = 0;

void dfs(int v, int d) {
  id[v] = it;
  table[0][it++] = {d, v};
  for (auto to : G[v]) {
    dfs(to, d + 1);
    table[0][it++] = {d, v};
  }
}

void init() {
  dfs(0, 0);
  int m = (n << 1) - 1;
  int h = 31 - __builtin_clz(m);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j + (1 << i) < m; j++) {
      table[i + 1][j] = min(table[i][j], table[i][j + (1 << i)]);
    }
  }
}

int lca(int u, int v) {
  u = id[u], v = id[v];
  if (u > v)
    swap(u, v);
  int b = 31 - __builtin_clz(v + 1 - u);
  return min(table[b][u], table[b][v + 1 - (1 << b)]).second;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      G[i].push_back(c);
    }
  }
  init();
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    printf("%d\\n", lca(u, v));
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;

const int MAX = 1e5;
pii table[17][MAX * 2 - 1];

int n;
vector<int> G[MAX];
int id[MAX];

int it = 0;

void dfs(int v, int d) {
  id[v] = it;
  table[0][it++] = {d, v};
  for (auto to : G[v]) {
    dfs(to, d + 1);
    table[0][it++] = {d, v};
  }
}

void init() {
  dfs(0, 0);
  int m = (n << 1) - 1;
  int h = 31 - __builtin_clz(m);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j + (1 << i) < m; j++) {
      table[i + 1][j] = min(table[i][j], table[i][j + (1 << i)]);
    }
  }
}

int lca(int u, int v) {
  u = id[u], v = id[v];
  if (u > v)
    swap(u, v);
  int b = 31 - __builtin_clz(v + 1 - u);
  return min(table[b][u], table[b][v + 1 - (1 << b)]).second;
}

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      G[i].push_back(c);
    }
  }
  init();
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    printf("%d\\n", lca(u, v));
  }
  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <vector>

using namespace std;

class path {
public:
  int cap;
  int dest;
  int flow;
  path(int cap_, int dest_) {
    cap = cap_;
    dest = dest_;
    flow = 0;
  }
};

int cap[100][100], flow[100][100], fsum, goal, vert;
bool visited[100];

int dfs(int cur, int minf) {
  if (cur == goal) {
    fsum += minf;
    return minf;
  }
  for (int i = 1; i < vert; i++) {
    if (!visited[i] && cap[cur][i] > flow[cur][i]) {
      visited[i] = true;
      int ret = dfs(i, min(minf, cap[cur][i] - flow[cur][i]));
      if (ret != 0) {
        flow[cur][i] += ret;
        return ret;
      }
    }
    if (!visited[i] && flow[i][cur] > 0) {
      visited[i] = true;
      int ret = dfs(i, min(minf, flow[i][cur]));
      if (ret != 0) {
        flow[i][cur] -= ret;
        return ret;
      }
    }
  }
  return 0;
}

int main() {
  int V, E;
  int u, v, c;
  cin >> V >> E;
  goal = V - 1;
  vert = V;
  for (int i = 0; i < E; i++) {
    cin >> u >> v >> c;
    cap[u][v] = c;
  }
  int ret = 0;
  while (ret = dfs(0, 10000)) {
    for (int i = 0; i < 100; i++)
      visited[i] = false;
  }
  cout << fsum << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

class path {
public:
  int cap;
  int dest;
  int flow;
  path(int cap_, int dest_) {
    cap = cap_;
    dest = dest_;
    flow = 0;
  }
};

int cap[100][100], flow[100][100], fsum, goal, vert;
bool visited[100];
int dfs(int cur, int minf) {
  if (cur == goal) {
    fsum += minf;
    return minf;
  }
  for (int i = 0; i < vert; i++) {
    if (!visited[i] && cap[cur][i] > flow[cur][i]) {
      visited[i] = true;
      int ret = dfs(i, min(minf, cap[cur][i] - flow[cur][i]));
      visited[i] = false;
      if (ret != 0) {
        flow[cur][i] += ret;
        return ret;
      }
    }
    if (!visited[i] && flow[i][cur] > 0) {
      visited[i] = true;
      int ret = dfs(i, min(minf, flow[i][cur]));
      visited[i] = false;
      if (ret != 0) {
        flow[i][cur] -= ret;
        return ret;
      }
    }
  }
  return 0;
}

int main() {
  int V, E;
  int u, v, c;
  cin >> V >> E;
  goal = V - 1;
  vert = V;
  for (int i = 0; i < E; i++) {
    cin >> u >> v >> c;
    cap[u][v] = c;
  }
  while (dfs(0, 10000))
    ;
  cout << fsum << endl;
}
Problem p02376 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 108
#define MAX_M 216000
#define EPS (1e-13)
#define INF (1LL << 28)
template <typename T> class MaxFlow {
private:
  struct Edge {
    int to, rev;
    T cap;
    Edge(int t, int r, T c) {
      to = t;
      rev = r;
      cap = c;
    }
  };

  vector<Edge> edges[MAX_N];
  bool done[MAX_N];

public:
  void add_edge(int from, int to, T f) {
    edges[from].push_back(Edge(to, edges[to].size(), f));
    edges[to].push_back(Edge(from, edges[from].size() - 1, 0));
  }

  T dfs(int now, int t, T f) {
    if (done[now])
      return 0;
    done[now] = true;
    if (now == t)
      return f;
    for (int i = 0; i < edges[now].size(); i++) {
      Edge &e = edges[now][i];
      if (e.cap < EPS)
        continue;
      T tmp = dfs(e.to, t, min(e.cap, f));
      if (tmp < EPS)
        continue;
      e.cap -= tmp;
      edges[e.to][e.rev].cap += tmp;
      return tmp;
    }
    return 0;
  }

  T max_flow(int s, int t) {
    T res = 0;
    while (true) {
      fill(done, done + MAX_N, false);
      T tmp = dfs(s, t, INF);
      if (tmp < EPS)
        return res;
      res += tmp;
    }
  }
};

int main() {
  int n, m, s, t, u, v, f;
  MaxFlow<int> mf;
  cin >> n >> m;
  s = 0;
  t = n - 1;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> f;
    mf.add_edge(u, v, f);
  }
  int tmp = mf.max_flow(s, t);
  cout << tmp << endl;
  return 0;
}


Error: 66

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 108
#define MAX_M 216000
#define EPS (1e-13)
#define INF (1LL << 28)
template <typename T> class MaxFlow {
private:
  struct Edge {
    int to, rev;
    T cap;
    Edge(int t, int r, T c) {
      to = t;
      rev = r;
      cap = c;
    }
  };

  vector<Edge> edges[MAX_N];
  bool done[MAX_N];

public:
  void add_edge(int from, int to, T f) {
    edges[from].push_back(Edge(to, edges[to].size(), f));
    edges[to].push_back(Edge(from, edges[from].size() - 1, 0));
  }

  T dfs(int now, int t, T f) {
    if (done[now])
      return 0;
    done[now] = true;
    if (now == t)
      return f;
    for (int i = 0; i < edges[now].size(); i++) {
      Edge &e = edges[now][i];
      if (e.cap == 0)
        exit(-190);
      if (e.cap < EPS)
        continue;
      T tmp = dfs(e.to, t, min(e.cap, f));
      if (tmp < EPS)
        continue;
      e.cap -= tmp;
      edges[e.to][e.rev].cap += tmp;
      return tmp;
    }
  }

  T max_flow(int s, int t) {
    T res = 0;
    while (true) {
      fill(done, done + MAX_N, false);
      T tmp = dfs(s, t, INF);
      if (tmp < EPS)
        return res;
      res += tmp;
    }
  }
};

int main() {
  int n, m, s, t, u, v, f;
  MaxFlow<int> mf;
  cin >> n >> m;
  s = 0;
  t = n - 1;
  /*for(int i = 0;i < m;i++){
    cin >> u >> v >> f;
    mf.add_edge(u, v, f);
  }*/
  mf.add_edge(s, t, 3);
  int tmp = mf.max_flow(s, t);
  cout << tmp << endl;
  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

const int inf = 1000000009;

int dfs(vector<vector<int>> &graph, bitset<100> &used, int flow, int pos,
        int t) {
  if (pos == t)
    return flow;
  used[pos] = 1;
  for (int i = 0; i < graph[pos].size(); i++) {
    if (used[i] == 0 && graph[pos][i] > 0) {
      int maxflow = dfs(graph, used, min(flow, graph[pos][i]), i, t);
      if (maxflow > 0) {
        graph[i][pos] = graph[i][pos] + maxflow;
        graph[pos][i] = graph[pos][i] - maxflow;
        return maxflow;
      }
    }
  }
  return 0;
}

int maxflow(vector<vector<int>> graph, int s, int t) {
  bool f = true;
  int sum = 0;
  while (f) {
    bitset<100> b(0);
    b[s] = 1;
    int current = dfs(graph, b, inf, s, t);
    sum += current;
    f &= current != 0;
  }
  return sum;
}

int main() {
  int v, e;
  cin >> v >> e;
  vector<vector<int>> graph(v);
  for (auto &l : graph)
    l.resize(v, 0);

  for (int i = 0; i < e; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    graph[u][v] = c;
  }

  cout << maxflow(graph, 0, v - 1) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

const int inf = 1000000009;

int dfs(vector<vector<int>> &graph, bitset<100> &used, int flow, int pos,
        int t) {
  if (pos == t)
    return flow;

  for (int i = 0; i < graph[pos].size(); i++) {
    if (used[i] == 0) {
      bitset<100> new_used = used;
      new_used[i] = 1;
      int maxflow = dfs(graph, new_used, min(flow, graph[pos][i]), i, t);
      if (maxflow != 0) {
        graph[i][pos] = graph[i][pos] + maxflow;
        graph[pos][i] = graph[pos][i] - maxflow;
        return maxflow;
      }
    }
  }
  return 0;
}

int maxflow(vector<vector<int>> graph, int s, int t) {
  bool f = true;
  int sum = 0;
  while (f) {
    bitset<100> b(0);
    b[s] = 1;
    int current = dfs(graph, b, inf, s, t);
    sum += current;
    f &= current != 0;
  }
  return sum;
}

int main() {
  int v, e;
  cin >> v >> e;
  vector<vector<int>> graph(v);
  for (auto &l : graph)
    l.resize(v, 0);

  for (int i = 0; i < e; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    graph[u][v] = c;
  }

  cout << maxflow(graph, 0, v - 1) << endl;
}
Problem p02376 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1e9;
struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

int v, e;
vector<vector<edge>> G; //??°???????????£??\\???????????¨???
bool used[1003];        // DFS??§??¢???????????????????????????????????°

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      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 maxflow(int s, int t) {
  int flow = 0;
  for (;;) {
    fill(used, used + e, false);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> v >> e;
  G.resize(v);
  REP(i, e) {
    int from, to, cap;
    cin >> from >> to >> cap;
    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));
  }
  cout << maxflow(0, v - 1) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1e9;
struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

int v, e;
vector<vector<edge>> G; //??°???????????£??\\???????????¨???
bool used[1003];        // DFS??§??¢???????????????????????????????????°

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      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 maxflow(int s, int t) {
  int flow = 0;
  for (;;) {
    fill(used, used + e, false);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> v >> e;
  G.resize(e);
  REP(i, e) {
    int from, to, cap;
    cin >> from >> to >> cap;
    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));
  }
  cout << maxflow(0, v - 1) << endl;
  return 0;
}
Problem p02376 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <memory>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct Node {
  int v;
  shared_ptr<Node> next;
};

void solve(int v, int e) {
  vector<vector<int>> capacities(v, vector<int>(v, 0));
  auto residual = [&](int from, int to) { return capacities[from][to]; };

  for (int i = 0; i < e; i++) {
    int from, to, capacity;
    cin >> from >> to >> capacity;
    capacities[from][to] = capacity;
  }

  int max_flow = 0;
  while (true) {
    stack<shared_ptr<Node>> paths;
    vector<int> visited(v, 0);
    paths.push(make_shared<Node>(Node{0, nullptr}));
    bool traveresed_all = false;
    while (!traveresed_all) {
      auto path = paths.top();
      paths.pop();
      auto last = path->v;
      if (!visited[last]) {
        visited[last] = 1;
        if (last == v - 1) {
          for (shared_ptr<Node> node = path; node->next != nullptr;
               node = node->next) {
            capacities[node->next->v][node->v]--;
            capacities[node->v][node->next->v]++;
          }
          max_flow++;
          break;
        }
        for (int i = 0; i < v; i++) {
          if (!visited[i] && residual(last, i) > 0) {
            auto new_path = make_shared<Node>(Node{i, path});
            paths.push(new_path);
          }
        }
      }
      traveresed_all = paths.empty();
    }
    if (traveresed_all) {
      cout << max_flow << endl;
      return;
    }
  }
}

int main() {
  int v, e;
  cin >> v >> e;
  solve(v, e);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

void solve(int v, int e) {
  vector<vector<int>> capacities(v, vector<int>(v, 0));
  auto residual = [&](int from, int to) { return capacities[from][to]; };

  for (int i = 0; i < e; i++) {
    int from, to, capacity;
    cin >> from >> to >> capacity;
    capacities[from][to] = capacity;
  }

  int max_flow = 0;
  while (true) {
    stack<vector<int>> paths;
    vector<int> visited(v, 0);
    paths.push({0});
    bool traveresed_all = false;
    while (!traveresed_all) {
      auto path = paths.top();
      paths.pop();
      auto last = path.back();
      if (!visited[last]) {
        visited[last] = 1;
        if (last == v - 1) {
          for (int i = 0; i < path.size() - 1; i++) {
            capacities[path[i]][path[i + 1]]--;
            capacities[path[i + 1]][path[i]]++;
          }
          max_flow++;
          break;
        }
        for (int i = 0; i < v; i++) {
          if (!visited[i] && residual(last, i) > 0) {
            auto new_path = path;
            new_path.push_back(i);
            paths.push(new_path);
          }
        }
      }
      traveresed_all = paths.empty();
    }
    if (traveresed_all) {
      cout << max_flow << endl;
      return;
    }
  }
}

int main() {
  int v, e;
  cin >> v >> e;
  solve(v, e);
}