Problem p03321 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 101;

int n;

int used[N];
int g[N][N];
int cl[N];

int fl = 0;

void dfs(int cur, int c, int &c1, int &c2) {
  cl[cur] = c;
  used[cur] = 1;
  if (c)
    c1++;
  else
    c2++;
  for (int i = 0; i < N; i++) {
    if (!used[i] && g[cur][i]) {
      dfs(i, c ^ 1, c1, c2);
    }
    if (used[i] && cl[cur] == cl[i] && g[cur][i]) {
      fl = 1;
    }
  }
}

void init() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      g[i][j] = (i != j);
    }
  }
}

vector<pair<int, int>> solve() {
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int c1 = 0;
      int c2 = 0;
      dfs(i, 0, c1, c2);
      v.push_back({c1, c2});
    }
  }
  if (fl) {
    cout << -1 << endl;
    exit(0);
  }
  return v;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int m;
  cin >> n >> m;
  init();
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u][v] ^= 1;
    g[v][u] ^= 1;
  }
  vector<pair<int, int>> v = solve();
  bitset<N> cn;
  cn[0] = 1;
  for (auto t : v) {
    cn = (cn << t.first) | (cn << t.second);
  }
  int ans = m;
  for (int lp = 0; lp <= n; lp++) {
    if (!cn[lp])
      continue;
    int rp = n - lp;
    ans = min(ans, lp * (lp - 1) / 2 + rp * (rp - 1) / 2);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 701;

int n;

int used[N];
int g[N][N];
int cl[N];

int fl = 0;

void dfs(int cur, int c, int &c1, int &c2) {
  cl[cur] = c;
  used[cur] = 1;
  if (c)
    c1++;
  else
    c2++;
  for (int i = 0; i < N; i++) {
    if (!used[i] && g[cur][i]) {
      dfs(i, c ^ 1, c1, c2);
    }
    if (used[i] && cl[cur] == cl[i] && g[cur][i]) {
      fl = 1;
    }
  }
}

void init() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      g[i][j] = (i != j);
    }
  }
}

vector<pair<int, int>> solve() {
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int c1 = 0;
      int c2 = 0;
      dfs(i, 0, c1, c2);
      v.push_back({c1, c2});
    }
  }
  if (fl) {
    cout << -1 << endl;
    exit(0);
  }
  return v;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int m;
  cin >> n >> m;
  init();
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u][v] ^= 1;
    g[v][u] ^= 1;
  }
  vector<pair<int, int>> v = solve();
  bitset<N> cn;
  cn[0] = 1;
  for (auto t : v) {
    cn = (cn << t.first) | (cn << t.second);
  }
  int ans = m;
  for (int lp = 0; lp <= n; lp++) {
    if (!cn[lp])
      continue;
    int rp = n - lp;
    ans = min(ans, lp * (lp - 1) / 2 + rp * (rp - 1) / 2);
  }
  cout << ans << endl;
}