Problem p02376 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF 1 << 30
#define LINF 1LL << 60

struct edge {
  int to;  // ?????????
  int cap; // ??????
  int rev; // ??????
  edge() {}
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

// from??????to??????????????????cap???????????°?????????????????????
void add_edge(int from, int to, int cap, vector<vector<edge>> &G) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

// ?¢?????????????DFS??§??¢???
int dfs(int v, int t, int f, vector<vector<edge>> &G, vector<int> &used) {
  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), G, used);
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// s??????t???????????§???????±???????
int max_flow(int s, int t, vector<vector<edge>> &G, vector<int> &used) {
  int flow = 0;
  for (;;) {
    for (int i = 0; i < used.size(); i++)
      used[i] = 0;
    int f = dfs(s, t, INF, G, used);
    if (f == 0)
      return flow;
    flow += f;
  }
}

/* use example */
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  vector<vector<edge>> G(V); // ??°???????????£??\\???????????¨???
  vector<int> used(V, 0);    // DFS??§?????§???????????????????????°
  for (int i = 0; i < E; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    add_edge(s, t, c, G);
  }
  cout << max_flow(0, V - 1, G, used) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF 1 << 30
#define LINF 1LL << 60

struct edge {
  int to;  // ?????????
  int cap; // ??????
  int rev; // ??????
  edge() {}
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

// from??????to??????????????????cap???????????°?????????????????????
void add_edge(int from, int to, int cap, vector<vector<edge>> &G) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[to].size() - 1));
}

// ?¢?????????????DFS??§??¢???
int dfs(int v, int t, int f, vector<vector<edge>> &G, vector<int> &used) {
  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), G, used);
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

// s??????t???????????§???????±???????
int max_flow(int s, int t, vector<vector<edge>> &G, vector<int> &used) {
  int flow = 0;
  for (;;) {
    fill(used.begin(), used.end(), 0);
    int f = dfs(s, t, INF, G, used);
    if (f == 0)
      return flow;
    flow += f;
  }
}

/* use example */
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E;
  cin >> V >> E;
  vector<vector<edge>> G(V); // ??°???????????£??\\???????????¨???
  vector<int> used(V, 0);    // DFS??§?????§???????????????????????°
  for (int i = 0; i < E; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    add_edge(s, t, c, G);
  }
  cout << max_flow(0, V - 1, G, used) << endl;
  return 0;
}