Problem p03321 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

namespace XDYIMCMI0LY {

const int N = 700 + 5;

int n;
int m;
int g[N][N];

int vis[N];
int col[N];
int cnt[2];

void dfs(int u, int nw) {
  vis[u] = 1;
  col[u] = nw;
  cnt[nw]++;
  for (int i = 1; i <= n; ++i) {
    if (g[u][i] && !vis[i]) {
      dfs(i, nw ^ 1);
    } else if (g[u][i]) {
      if (col[i] == col[u]) {
        puts("-1");
        exit(0);
      }
    }
  }
}

int dp[N][N][N];

int main() {
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; ++i) {
    cin >> u >> v;
    g[u][v] = g[v][u] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      g[i][j] ^= 1;
      g[j][i] ^= 1;
    }
  }
  memset(col, -1, sizeof(col));
  vector<pair<int, int>> v;
  v.push_back(make_pair(0, 0));
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      cnt[0] = cnt[1] = 0;
      dfs(i, 0);
      v.push_back(make_pair(cnt[0], cnt[1]));
    }
  }
  m = n;
  n = v.size();
  dp[0][0][0] = 1;
  for (int i = 1; i < n; ++i) {
    int x = v[i].first;
    int y = v[i].second;
    for (int j = 0; j <= m; ++j) {
      for (int k = 0; k <= m; ++k) {
        if (j - x >= 0 && k - y >= 0) {
          dp[i][j][k] |= dp[i - 1][j - x][k - y];
        }
        if (j - y >= 0 && k - x >= 0) {
          dp[i][j][k] |= dp[i - 1][j - y][k - x];
        }
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j <= m; ++j) {
      if (dp[n - 1][i][j]) {
        ans = min(ans, i * (i - 1) / 2 + j * (j - 1) / 2);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
} // namespace XDYIMCMI0LY

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  XDYIMCMI0LY::main();
  return 0;
}


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

using namespace std;

namespace XDYIMCMI0LY {

const int N = 700 + 5;

int n;
int m;
int g[N][N];

int vis[N];
int col[N];
int cnt[2];

void dfs(int u, int nw) {
  vis[u] = 1;
  col[u] = nw;
  cnt[nw]++;
  for (int i = 1; i <= n; ++i) {
    if (g[u][i] && !vis[i]) {
      dfs(i, nw ^ 1);
    } else if (g[u][i]) {
      if (col[i] == col[u]) {
        puts("-1");
        exit(0);
      }
    }
  }
}

bool dp[N][N][N];

int main() {
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; ++i) {
    cin >> u >> v;
    g[u][v] = g[v][u] = 1;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      g[i][j] ^= 1;
      g[j][i] ^= 1;
    }
  }
  memset(col, -1, sizeof(col));
  vector<pair<int, int>> v;
  v.push_back(make_pair(0, 0));
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      cnt[0] = cnt[1] = 0;
      dfs(i, 0);
      v.push_back(make_pair(cnt[0], cnt[1]));
    }
  }
  m = n;
  n = v.size();
  dp[0][0][0] = 1;
  for (int i = 1; i < n; ++i) {
    int x = v[i].first;
    int y = v[i].second;
    for (int j = 0; j <= m; ++j) {
      for (int k = 0; k <= m; ++k) {
        if (j - x >= 0 && k - y >= 0) {
          dp[i][j][k] |= dp[i - 1][j - x][k - y];
        }
        if (j - y >= 0 && k - x >= 0) {
          dp[i][j][k] |= dp[i - 1][j - y][k - x];
        }
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j <= m; ++j) {
      if (dp[n - 1][i][j]) {
        ans = min(ans, i * (i - 1) / 2 + j * (j - 1) / 2);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
} // namespace XDYIMCMI0LY

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  XDYIMCMI0LY::main();
  return 0;
}