Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 11000000
#define INF 0x3f3f3f3f
#define M 7031004
struct node {
  int to, next, res;
} q[M];
int head[22003], ss, prm[N], dep[23104], ct, a[N], S, T, que[20310], h[20310],
    cir[20104];
bool ip[N];
void addedge(int x, int y, int r) {
  q[++ss] = (node){y, head[x], r};
  head[x] = ss;
  q[++ss] = (node){x, head[y], 0};
  head[y] = ss;
}
bool bfs() {
  memset(dep, 0, sizeof(dep));
  memcpy(cir, head, 4 * (T + 10));
  dep[S] = 1;
  int f = 1, e = 0;
  que[++e] = S;
  while (f <= e) {
    int u = que[f++];
    for (int j = head[u]; ~j; j = q[j].next)
      if (q[j].res && !dep[q[j].to]) {
        int t = q[j].to;
        dep[t] = dep[u] + 1;
        que[++e] = t;
      }
  }
  return dep[T] > 0;
}
int dfs(int i, int flow) {
  if (i == T || !flow)
    return flow;
  int f, dist = 0;
  for (int &j = cir[i]; ~j; j = q[j].next)
    if (dep[q[j].to] == dep[i] + 1 && q[j].res &&
        (f = dfs(q[j].to, min(q[j].res, flow - dist)))) {
      dist += f;
      q[j].res -= f;
      q[j ^ 1].res += f;
    }
  return dist;
}
int dinic() {
  int ans = 0;
  while (bfs())
    ans += dfs(S, INF);
  return ans;
}
int main() {
  int n, TT, c = 0, ans = 0, odd;
  scanf("%d", &TT);
  ip[1] = true;
  ip[0] = true;
  for (int i = 2; i <= 10000000; ++i) {
    if (!ip[i])
      prm[++ct] = i;
    for (int j = 1, to; j <= ct && (to = i * prm[j]) <= 10000000; ++j) {
      ip[to] = true;
      if (i % prm[j] == 0)
        break;
    }
  }
  ip[2] = true;
  a[0] = -INF;
  while (TT--) {
    scanf("%d", &n);
    c = 0;
    ss = -1;
    odd = 0;
    memset(head, -1, sizeof(head));
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      if (a[i] != a[i - 1] + 1)
        h[++c] = a[i];
      h[a[i] == a[i - 1] + 1 ? c : ++c] = a[i] + 1;
    }
    T = c + 1;
    for (int i = 1; i <= c; ++i)
      if (h[i] & 1) {
        addedge(S, i, 1);
        ++odd;
        for (int j = 1; j <= c; ++j) {
          int dis = abs(h[j] - h[i]);
          if (!ip[dis])
            addedge(i, j, 1);
        }
      } else
        addedge(i, T, 1);
    ans = dinic();
    c -= odd;
    odd -= ans;
    printf("%d\n", ans + (odd / 2 * 2) + (c - ans) / 2 * 2 + (odd & 1 ? 3 : 0));
  }
  return 0;
}

// by qlwpc

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 11000000
#define INF 0x3f3f3f3f
#define M 7031004
struct node {
  int to, next, res;
} q[M];
int head[22003], ss, prm[N], dep[23104], ct, a[N], S, T, que[20310], h[20310],
    cir[20104];
bool ip[N];
void addedge(int x, int y, int r) {
  q[++ss] = (node){y, head[x], r};
  head[x] = ss;
  q[++ss] = (node){x, head[y], 0};
  head[y] = ss;
}
bool bfs() {
  memset(dep, 0, sizeof(dep));
  memcpy(cir, head, 4 * (T + 10));
  dep[S] = 1;
  int f = 1, e = 0;
  que[++e] = S;
  while (f <= e) {
    int u = que[f++];
    for (int j = head[u]; ~j; j = q[j].next)
      if (q[j].res && !dep[q[j].to]) {
        int t = q[j].to;
        dep[t] = dep[u] + 1;
        que[++e] = t;
      }
  }
  return dep[T] > 0;
}
int dfs(int i, int flow) {
  if (i == T || !flow)
    return flow;
  int f, dist = 0;
  for (int &j = cir[i]; ~j; j = q[j].next)
    if (dep[q[j].to] == dep[i] + 1 && q[j].res &&
        (f = dfs(q[j].to, min(q[j].res, flow - dist)))) {
      dist += f;
      q[j].res -= f;
      q[j ^ 1].res += f;
    }
  return dist;
}
int dinic() {
  int ans = 0;
  while (bfs())
    ans += dfs(S, INF);
  return ans;
}
int main() {
  int n, TT = 1, c = 0, ans = 0, odd;
  ip[1] = true;
  ip[0] = true;
  for (int i = 2; i <= 10000000; ++i) {
    if (!ip[i])
      prm[++ct] = i;
    for (int j = 1, to; j <= ct && (to = i * prm[j]) <= 10000000; ++j) {
      ip[to] = true;
      if (i % prm[j] == 0)
        break;
    }
  }
  ip[2] = true;
  a[0] = -INF;
  while (TT--) {
    scanf("%d", &n);
    c = 0;
    ss = -1;
    odd = 0;
    memset(head, -1, sizeof(head));
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      if (a[i] != a[i - 1] + 1)
        h[++c] = a[i];
      h[a[i] == a[i - 1] + 1 ? c : ++c] = a[i] + 1;
    }
    T = c + 1;
    for (int i = 1; i <= c; ++i)
      if (h[i] & 1) {
        addedge(S, i, 1);
        ++odd;
        for (int j = 1; j <= c; ++j) {
          int dis = abs(h[j] - h[i]);
          if (!ip[dis])
            addedge(i, j, 1);
        }
      } else
        addedge(i, T, 1);
    ans = dinic();
    c -= odd;
    odd -= ans;
    printf("%d\n", ans + (odd / 2 * 2) + (c - ans) / 2 * 2 + (odd & 1 ? 3 : 0));
  }
  return 0;
}

// by qlwpc