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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

#define MAX_V 110

struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V]; //??°???????????????
int level[MAX_V];      //?§??????????????????¢
int iter[MAX_V];       //???????????§????????????

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

// s????????????????????¢???BFS??§?¨??????????
void bfs(int s) {
  rep(i, MAX_V) level[i] = -1;
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    rep(i, G[v].size()) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

//?¢?????????????DFS??§??¢???
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;
}

// s??????t???????????§???????±???????
int max_flow(int s, int t) {
  if (s == t)
    return INF;
  int flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

void isUsedDfs(int s, int t, bool used[MAX_V]) {
  if (s == t)
    return;
  for (auto &e : G[s]) {
    if (!used[e.to] && e.cap > 0) {
      used[e.to] = true;
      isUsedDfs(e.to, t, used);
    }
  }
}

int main() {
  int k, n, m;
  cin >> k >> n >> m;
  int s = 1 + k + n;
  int t = 0;
  int v = s + 1;

  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    add_edge(a, b, c);
    add_edge(b, a, c);
  }

  rep(i, k) add_edge(s, i + 1, INF);

  int res = max_flow(s, t);
  vector<edge> Greg[MAX_V];
  rep(i, v) Greg[i] = G[i];

  bool used[MAX_V] = {};
  used[s] = true;
  isUsedDfs(s, t, used);

  int ans = 0;
  vector<int> sflow(v, -1), tflow(v, -1);
  rep(i, v) {
    for (auto e : Greg[i]) {
      if (used[i] && !used[e.to]) {
        rep(i, v) G[i] = Greg[i];
        if (sflow[i] == -1)
          sflow[i] = max_flow(s, i);
        if (tflow[e.to] == -1)
          tflow[e.to] = max_flow(e.to, t);
        ans = max(ans, res + min(sflow[i], tflow[e.to]));
      }
    }
  }
  if (ans >= INF)
    cout << "overfuro" << endl;
  else
    cout << ans << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

#define MAX_V 100

struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V]; //??°???????????????
int level[MAX_V];      //?§??????????????????¢
int iter[MAX_V];       //???????????§????????????

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

// s????????????????????¢???BFS??§?¨??????????
void bfs(int s) {
  rep(i, MAX_V) level[i] = -1;
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();

    rep(i, G[v].size()) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

//?¢?????????????DFS??§??¢???
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;
}

// s??????t???????????§???????±???????
int max_flow(int s, int t) {
  if (s == t)
    return INF;
  int flow = 0;
  for (;;) {
    bfs(s);
    if (level[t] < 0)
      return flow;
    memset(iter, 0, sizeof(iter));
    int f;
    while ((f = dfs(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

void isUsedDfs(int s, int t, bool used[MAX_V]) {
  if (s == t)
    return;
  for (auto &e : G[s]) {
    if (!used[e.to] && e.cap > 0) {
      used[e.to] = true;
      isUsedDfs(e.to, t, used);
    }
  }
}

int main() {
  int k, n, m;
  cin >> k >> n >> m;
  int s = 1 + k + n;
  int t = 0;
  int v = s + 1;

  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    add_edge(a, b, c);
    add_edge(b, a, c);
  }

  rep(i, k) add_edge(s, i + 1, INF);

  int res = max_flow(s, t);
  vector<edge> Greg[MAX_V];
  rep(i, v) Greg[i] = G[i];

  bool used[MAX_V] = {};
  used[s] = true;
  isUsedDfs(s, t, used);

  int ans = 0;
  vector<int> sflow(v, -1), tflow(v, -1);
  rep(i, v) {
    for (auto e : Greg[i]) {
      if (used[i] && !used[e.to]) {
        rep(i, v) G[i] = Greg[i];
        if (sflow[i] == -1)
          sflow[i] = max_flow(s, i);
        if (tflow[e.to] == -1)
          tflow[e.to] = max_flow(e.to, t);
        ans = max(ans, res + min(sflow[i], tflow[e.to]));
      }
    }
  }
  if (ans >= INF)
    cout << "overfuro" << endl;
  else
    cout << ans << endl;
}