Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int to, cap, rev;
};
#define MAX_V (102)
#define INF (2000000000)

vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

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});
}

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    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(f, e.cap));
      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 (true) {
    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 is_prime(int n) {
  if (1 == n)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (0 == n % i)
      return false;
  return true;
}

int main() {
  int N;
  cin >> N;
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  vector<int> p;
  for (int i = 0; i < N; i++) {
    p.push_back(x[i]);
    for (; i < N - 1 && x[i] + 1 == x[i + 1]; i++)
      ; /* 連続スキップ */
    p.push_back(x[i] + 1);
  }
  int s = MAX_V - 2;
  int t = s + 1;
  int num = p.size();
  int evenNum = 0;
  for (int i = 0; i < num; i++) {
    if (0 == (p[i] % 2)) {
      add_edge(s, i, 1);
      evenNum++;
      for (int j = 0; j < num; j++) {
        int dst = abs(p[i] - p[j]);
        if (dst > 2 && is_prime(dst))
          add_edge(i, j, 1);
      }
    } else {
      add_edge(i, t, 1);
    }
  }
  int f = max_flow(s, t);
  int ans = f;
  ans += ((evenNum - f) / 2 + (num - evenNum - f) / 2) * 2;
  ans += ((evenNum - f) % 2) * 3;
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct edge {
  int to, cap, rev;
};
#define MAX_V (202)
#define INF (1000000000)

vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

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});
}

void bfs(int s) {
  memset(level, -1, sizeof(level));
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    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(f, e.cap));
      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 (true) {
    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 is_prime(int n) {
  if (1 == n)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (0 == n % i)
      return false;
  return true;
}

int main() {
  int N;
  cin >> N;
  vector<int> x(N);
  for (int i = 0; i < N; i++) {
    cin >> x[i];
  }
  vector<int> p;
  for (int i = 0; i < N; i++) {
    p.push_back(x[i]);
    for (; i < N - 1 && x[i] + 1 == x[i + 1]; i++)
      ; /* 連続スキップ */
    p.push_back(x[i] + 1);
  }
  int s = MAX_V - 2;
  int t = s + 1;
  int num = p.size();
  int evenNum = 0;
  for (int i = 0; i < num; i++) {
    if (0 == (p[i] % 2)) {
      add_edge(s, i, 1);
      evenNum++;
      for (int j = 0; j < num; j++) {
        int dst = abs(p[i] - p[j]);
        if (dst > 2 && is_prime(dst))
          add_edge(i, j, 1);
      }
    } else {
      add_edge(i, t, 1);
    }
  }
  int f = max_flow(s, t);
  int ans = f;
  ans += ((evenNum - f) / 2 + (num - evenNum - f) / 2) * 2;
  ans += ((evenNum - f) % 2) * 3;
  cout << ans << endl;
}