Problem p03321 (C++)

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

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define s second

using namespace std;

const int N = (int)770 + 10;
const int K = (int)N * N;
const int mod = (int)1e9 + 7;

int n, m, comp;
bool g[N][N], u[N];
int c[N];
int comp_id[N];
bool dp[N], gdp[N];
int cnt[2];

void dfs(int x, int clr = 0) {
  // cout << x << ' ' << clr << ' ' << comp << endl;
  u[x] = 1;
  c[x] = clr;
  cnt[clr]++;
  comp_id[x] = comp;
  for (int i = 1; i <= n; i++) {
    if (g[x][i] || i == x)
      continue;
    if (!u[i]) {
      dfs(i, clr ^ 1);
    } else if ((clr ^ 1) != c[i]) {
      cout << -1;
      exit(0);
    }
  }
}

ll f(ll x) { return x * (x - 1) / 2; }

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> m;
  for (int i = 1, x, y; i <= n; i++) {
    cin >> x >> y;
    g[x][y] = g[y][x] = 1;
  }
  vector<pii> v;
  for (int i = 1; i <= n; i++) {
    if (!u[i]) {
      ++comp;
      dfs(i);
      v.pb(mp(cnt[0], cnt[1]));
      cnt[0] = cnt[1] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (comp_id[i] != comp_id[j] && g[i][j] == 0) {
        while (1) {
        }
      }
    }
  }
  dp[0] = 1;
  for (auto it : v) {
    memset(gdp, 0, sizeof(gdp));
    for (int i = n; i >= 0; i--) {
      if (i + it.f <= n) {
        gdp[i + it.f] |= dp[i];
      }
      if (i + it.s <= n) {
        gdp[i + it.s] |= dp[i];
      }
    }
    for (int i = n; i >= 0; i--) {
      dp[i] = gdp[i];
    }
  }
  ll ans = LLONG_MAX;
  for (int i = n; i >= 0; i--) {
    if (dp[i]) {
      ans = min(ans, f(i) + f(n - i));
    }
  }
  cout << ans;
  return 0;
}

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

#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define s second

using namespace std;

const int N = (int)770 + 10;
const int K = (int)N * N;
const int mod = (int)1e9 + 7;

int n, m, comp;
bool g[N][N], u[N];
int c[N];
int comp_id[N];
bool dp[N], gdp[N];
int cnt[2];

void dfs(int x, int clr = 0) {
  // cout << x << ' ' << clr << ' ' << comp << endl;
  u[x] = 1;
  c[x] = clr;
  cnt[clr]++;
  comp_id[x] = comp;
  for (int i = 1; i <= n; i++) {
    if (g[x][i] || i == x)
      continue;
    if (!u[i]) {
      dfs(i, clr ^ 1);
    } else if ((clr ^ 1) != c[i]) {
      cout << -1;
      exit(0);
    }
  }
}

ll f(ll x) { return x * (x - 1) / 2; }

int main() {
#ifdef sony
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> m;
  for (int i = 1, x, y; i <= m; i++) {
    cin >> x >> y;
    g[x][y] = g[y][x] = 1;
  }
  vector<pii> v;
  for (int i = 1; i <= n; i++) {
    if (!u[i]) {
      ++comp;
      dfs(i);
      v.pb(mp(cnt[0], cnt[1]));
      cnt[0] = cnt[1] = 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (comp_id[i] != comp_id[j] && g[i][j] == 0) {
        while (1) {
        }
      }
    }
  }
  dp[0] = 1;
  for (auto it : v) {
    memset(gdp, 0, sizeof(gdp));
    for (int i = n; i >= 0; i--) {
      if (i + it.f <= n) {
        gdp[i + it.f] |= dp[i];
      }
      if (i + it.s <= n) {
        gdp[i + it.s] |= dp[i];
      }
    }
    for (int i = n; i >= 0; i--) {
      dp[i] = gdp[i];
    }
  }
  ll ans = LLONG_MAX;
  for (int i = n; i >= 0; i--) {
    if (dp[i]) {
      ans = min(ans, f(i) + f(n - i));
    }
  }
  cout << ans;
  return 0;
}