Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e7 + 700;

int n, fre[maxn], p[220], ispr[maxn], dd[maxn], even, odd, wrong;
vector<int> edge[220], v2;

bool fin(int u) {
  if (fre[u] == wrong)
    return false;
  fre[u] = wrong;
  for (int j = 0; j < edge[u].size(); j++) {
    int v = edge[u][j];
    // cout<<u<<" "<<v<<endl;
    if (p[v] == -1 || fin(p[v])) {
      p[v] = u;
      return true;
    }
  }
  return false;
}

int main() {
  freopen("in.txt", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ispr[1] = 1;
  for (int i = 2; i < maxn; i++)
    if (ispr[i] == 0) {
      for (int j = i + i; j < maxn; j += i)
        ispr[j] = 1;
    }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (dd[x] == 1)
      continue;
    dd[x] = 1;
    fre[x] ^= 1;
    fre[x + 1] ^= 1;
  }
  even = 0;
  odd = 0;
  wrong = 2;
  for (int i = 0; i < maxn; i++)
    if (fre[i] == 1) {
      v2.push_back(i);
      if (i % 2 == 0)
        even++;
      else
        odd++;
    }
  // for (int i=0;i<v2.size();i++) cout<<v2[i]<<" ";
  //  cout<<endl;
  for (int i = 0; i < v2.size(); i++)
    for (int j = 0; j < v2.size(); j++)
      if (v2[i] % 2 == 0 && v2[j] % 2 == 1 && ispr[abs(v2[j] - v2[i])] == 0 &&
          abs(v2[j] - v2[i]) % 2 == 1) {
        edge[i].push_back(j);
        // cout<<i<<" "<<j<<" "<<v2[i]<<" "<<v2[j]<<endl;
      }
  int cnt = 0;
  for (int i = 0; i < v2.size(); i++)
    if (v2[i] % 2 == 1)
      p[i] = -1;
  for (int i = 0; i < v2.size(); i++)
    if (v2[i] % 2 == 0) {
      wrong++;
      cnt += fin(i);
    }
  //   cout<<" " <<even<<" "<<odd<<" "<<cnt<<endl;
  cout << cnt + (even - cnt) / 2 * 2 + (odd - cnt) / 2 * 2 +
              (even - cnt) % 2 * 3;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e7 + 700;

int n, fre[maxn], p[220], ispr[maxn], dd[maxn], even, odd, wrong;
vector<int> edge[220], v2;

bool fin(int u) {
  if (fre[u] == wrong)
    return false;
  fre[u] = wrong;
  for (int j = 0; j < edge[u].size(); j++) {
    int v = edge[u][j];
    // cout<<u<<" "<<v<<endl;
    if (p[v] == -1 || fin(p[v])) {
      p[v] = u;
      return true;
    }
  }
  return false;
}

int main() {
  //   freopen("in.txt","r",stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ispr[1] = 1;
  for (int i = 2; i < maxn; i++)
    if (ispr[i] == 0) {
      for (int j = i + i; j < maxn; j += i)
        ispr[j] = 1;
    }
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (dd[x] == 1)
      continue;
    dd[x] = 1;
    fre[x] ^= 1;
    fre[x + 1] ^= 1;
  }
  even = 0;
  odd = 0;
  wrong = 2;
  for (int i = 0; i < maxn; i++)
    if (fre[i] == 1) {
      v2.push_back(i);
      if (i % 2 == 0)
        even++;
      else
        odd++;
    }
  // for (int i=0;i<v2.size();i++) cout<<v2[i]<<" ";
  //  cout<<endl;
  for (int i = 0; i < v2.size(); i++)
    for (int j = 0; j < v2.size(); j++)
      if (v2[i] % 2 == 0 && v2[j] % 2 == 1 && ispr[abs(v2[j] - v2[i])] == 0 &&
          abs(v2[j] - v2[i]) % 2 == 1) {
        edge[i].push_back(j);
        // cout<<i<<" "<<j<<" "<<v2[i]<<" "<<v2[j]<<endl;
      }
  int cnt = 0;
  for (int i = 0; i < v2.size(); i++)
    if (v2[i] % 2 == 1)
      p[i] = -1;
  for (int i = 0; i < v2.size(); i++)
    if (v2[i] % 2 == 0) {
      wrong++;
      cnt += fin(i);
    }
  //   cout<<" " <<even<<" "<<odd<<" "<<cnt<<endl;
  cout << cnt + (even - cnt) / 2 * 2 + (odd - cnt) / 2 * 2 +
              (even - cnt) % 2 * 3;
}