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

#include <bits/stdc++.h>
#define MOD 1000000007LL
#define INF 1000000
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

struct edge {
  int to, cap, rev, ng;
  edge() {}
  edge(int tt, int cc, int rr, int nng) {
    to = tt;
    cap = cc;
    rev = rr;
    ng = nng;
  }
};

vector<edge> G[10000];
vector<int> id[10001];
int level[10000];
int iter[10000];
bool used[10001];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size(), cap));
  if (cap == 1)
    id[from].push_back(G[from].size() - 1);
  G[to].push_back(edge(from, cap, G[from].size() - 1, cap));
  if (cap == 1)
    id[to].push_back(G[to].size() - 1);
}

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

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(e.cap, f));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

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

bool get_path(int v, int t, int p) {
  if (v == t)
    return true;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i].cap > 0 && p != G[v][i].to && !used[G[v][i].to]) {
      if (get_path(G[v][i].to, t, v))
        return true;
    }
  }
  return false;
}

int n, m;

int s[5005], t[5005], c[5005];
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &s[i], &t[i], &c[i]);
    add_edge(s[i], t[i], c[i]);
  }
  int val = max_flow(0, n - 1);
  /*
  for(int i=0;i<n;i++){
          G[i].clear();
  }
  for(int i=0;i<m;i++){
          if(c[i]>=2)add_edge(s[i],t[i],c[i]);
          else add_edge(s[i],t[i],0);
  }
  int val2=max_flow(0,n-1);
  if(val!=val2)val--;
  */
  bool flag2 = false;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < id[i].size(); j++) {
      cnt++;
      if (G[i][id[i][j]].cap == 0) {
        memset(used, false, sizeof(used));
        if (!flag2 && !get_path(i, G[i][id[i][j]].to, -1)) {
          flag2 = true;
          val--;
        }
      }
    }
  }
  printf("%d\\n", val <= 10000 ? val : -1);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
#define INF 1000000
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

struct edge {
  int to, cap, rev, ng;
  edge() {}
  edge(int tt, int cc, int rr, int nng) {
    to = tt;
    cap = cc;
    rev = rr;
    ng = nng;
  }
};

vector<edge> G[10000];
vector<int> id[10001];
int level[10000];
int iter[10000];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size(), cap));
  if (cap == 1)
    id[from].push_back(G[from].size() - 1);
  G[to].push_back(edge(from, cap, G[from].size() - 1, cap));
}

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (que.size()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

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(e.cap, f));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

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

bool get_path(int v, int t, int p) {
  if (v == t)
    return true;
  for (int i = 0; i < G[v].size(); i++) {
    if (G[v][i].cap > 0 && p != G[v][i].to) {
      if (get_path(G[v][i].to, t, v))
        return true;
    }
  }
  return false;
}

int n, m;

int s[5005], t[5005], c[5005];
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &s[i], &t[i], &c[i]);
    add_edge(s[i], t[i], c[i]);
  }
  int val = max_flow(0, n - 1);
  /*
  for(int i=0;i<n;i++){
          G[i].clear();
  }
  for(int i=0;i<m;i++){
          if(c[i]>=2)add_edge(s[i],t[i],c[i]);
          else add_edge(s[i],t[i],0);
  }
  int val2=max_flow(0,n-1);
  if(val!=val2)val--;
  */
  bool flag = false;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < id[i].size(); j++) {
      cnt++;
      if (G[i][id[i][j]].cap == 0) {
        if (!flag && !get_path(i, G[i][id[i][j]].to, -1)) {
          flag = true;
          val--;
        }
      }
    }
  }
  printf("%d\\n", val <= 10000 ? val : -1);
  return 0;
}