Problem p03321 (C++)

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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
vector<int> edge[1000];
vector<int> edge2[1000];
bool e[1000][1000] = {};
int visited[1000] = {};
int n, m;
bool flag = true;
int bma = 0, wma = 0;
void dfs(int now, int color) {
  if (visited[now] == -color) {
    flag = false;
  }
  if (visited[now] == color) {
    return;
  }
  visited[now] = color;
  if (color < 0)
    bma++;
  else
    wma++;
  for (int i = 0; i < edge2[now].size(); i++) {
    dfs(edge2[now][i], -color);
  }
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    edge[--a].pb(--b);
    edge[b].pb(a);
    e[a][b] = true;
    e[b][a] = true;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((i == j) || (e[i][j]))
        continue;
      edge2[i].pb(j);
      // cout<<i<<" "<<j<<endl;
    }
  }
  vector<pair<int, int>> vpii;
  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      // cout<<i<<endl;
      dfs(i, 1);
      vpii.pb(mp(bma, wma));
      bma = 0;
      wma = 0;
    }
  }
  // cout<<vpii.size()<<endl;
  if (flag == false) {
    cout << -1 << endl;
  } else {
    bool dp[2][n + 700];
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < n + 700; j++)
        dp[i][j] = false;
    dp[0][0] = true;
    for (int i = 1; i <= vpii.size(); i++) {
      for (int j = 0; j <= n; j++) {
        if (dp[(i + 1) % 2][j]) {
          //  cout<<i<<" "<<j<<endl;
          dp[i % 2][j + vpii[i - 1].F] = true;
          dp[i % 2][j + vpii[i - 1].S] = true;
        }
        dp[(i + 1) % 2][j] = false;
      }
    }
    int ans = INF;
    for (int i = 0; i <= n; i++) {
      if (dp[vpii.size() % 2][i]) {
        ans = min(ans, ((i - 1) * i / 2) + ((n - i) * (n - i - 1)) / 2);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
vector<int> edge[1000];
vector<int> edge2[1000];
bool e[1000][1000] = {};
int visited[1000] = {};
int n, m;
bool flag = true;
int bma = 0, wma = 0;
void dfs(int now, int color) {
  if (visited[now] == -color) {
    flag = false;
    return;
  }
  if (visited[now] == color) {
    return;
  }
  visited[now] = color;
  if (color < 0)
    bma++;
  else
    wma++;
  for (int i = 0; i < edge2[now].size(); i++) {
    dfs(edge2[now][i], -color);
  }
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    edge[--a].pb(--b);
    edge[b].pb(a);
    e[a][b] = true;
    e[b][a] = true;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((i == j) || (e[i][j]))
        continue;
      edge2[i].pb(j);
      // cout<<i<<" "<<j<<endl;
    }
  }
  vector<pair<int, int>> vpii;
  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      // cout<<i<<endl;
      dfs(i, 1);
      vpii.pb(mp(bma, wma));
      bma = 0;
      wma = 0;
    }
  }
  // cout<<vpii.size()<<endl;
  if (flag == false) {
    cout << -1 << endl;
  } else {
    bool dp[2][n + 700];
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < n + 700; j++)
        dp[i][j] = false;
    dp[0][0] = true;
    for (int i = 1; i <= vpii.size(); i++) {
      for (int j = 0; j <= n; j++) {
        if (dp[(i + 1) % 2][j]) {
          //  cout<<i<<" "<<j<<endl;
          dp[i % 2][j + vpii[i - 1].F] = true;
          dp[i % 2][j + vpii[i - 1].S] = true;
        }
        dp[(i + 1) % 2][j] = false;
      }
    }
    int ans = INF;
    for (int i = 0; i <= n; i++) {
      if (dp[vpii.size() % 2][i]) {
        ans = min(ans, ((i - 1) * i / 2) + ((n - i) * (n - i - 1)) / 2);
      }
    }
    cout << ans << endl;
  }
  return 0;
}