Problem p03321 (C++)

Buggy submission (status: Runtime Error):
/*input
10 39
7 2
7 1
5 6
5 8
9 10
2 8
8 7
3 10
10 1
8 10
2 3
7 4
3 9
4 10
3 4
6 1
6 7
9 5
9 7
6 9
9 4
4 6
7 5
8 3
2 5
9 2
10 7
8 6
8 9
7 3
5 3
4 5
6 3
2 10
5 10
4 2
6 2
8 4
10 6
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i, n) for (int i = 1; i <= n; i++)
#define RED(i, n) for (int i = n; i > 0; i--)
#define REPS(i, n) for (int i = 1; (i * i) <= n; i++)
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)

#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define print(arr)                                                             \
  for (auto it = arr.begin(); it != arr.end(); ++it)                           \
    cout << *it << " ";                                                        \
  cout << endl;
#define debug(x) cout << x << endl;
#define debug2(x, y) cout << x << " " << y << endl;
#define debug3(x, y, z) cout << x << " " << y << " " << z << endl;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int INF = 1e18 + 1;
const int MOD = 1e9 + 7;
const double PI = 3.14159265358979323846264338;

int raise(int a, int n, int m = MOD) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  int x = 1;
  x *= raise(a, n / 2, m);
  x %= m;
  x *= x;
  x %= m;
  if (n % 2)
    x *= a;
  x %= m;
  return x;
}

int floor1(int n, int k) {
  if (n % k == 0 || n >= 0)
    return n / k;
  return (n / k) - 1;
}

int ceil1(int n, int k) { return floor1(n + k - 1, k); }

const int N = 769;
int mat[N][N];
int n, m;
int ans = 0;
// ans = -comp(l)*comp(r) + L*R
// N-R*R = N*R - R*R
int color[N];
int l[N];
int r[N];
int comp = 0;
bool f = 0;
bool dp[N][N]; // can we have sum j till i th index ?

void bfs(int source) {
  queue<int> q;
  q.push(source);
  color[source] = 1;
  int red = 1;
  int blue = 0;
  int edges = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v = 1; v <= n; v++) {
      if (mat[u][v])
        edges++;
      if (!mat[u][v])
        continue;
      if (color[v] == color[u])
        f = 1;
      if (color[v])
        continue;
      color[v] = (color[u] == 1) ? 2 : 1;
      (color[v] == 1) ? red++ : blue++;
      q.push(v);
    }
  }
  comp++;
  l[comp] = blue;
  r[comp] = red;
}

void solve() {
  cin >> n >> m;
  RE(i, n) {
    color[i] = 0;
    RE(j, n) {
      mat[i][j] = 0;
      dp[i][j] = 0;
    }
  }
  RE(i, m) {
    int a, b;
    cin >> a >> b;
    mat[a][b] = mat[b][a] = 1;
  }
  RE(i, n) {
    RE(j, n) {
      if (i != j and !mat[i][j])
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
    }
  }
  RE(i, n) {
    if (!color[i])
      bfs(i);
  }
  if (f) {
    cout << -1;
    return;
  }
  int mx = 0;
  dp[0][0] = 1;
  for (int i = 1; i <= comp; i++) {
    // cout << l[i] << " " << r[i] << endl;
    for (int j = 0; j <= n; j++) {
      // dp[i][j] = dp[i-1][j];
      if (j >= r[i])
        dp[i][j] |= dp[i - 1][j - r[i]];
      if (j >= l[i])
        dp[i][j] |= dp[i - 1][j - l[i]];
    }
  }
  int cur = INF;
  int nchoose = n * (n - 1);
  nchoose /= 2;
  for (int i = 0; i <= n; i++) {
    if (dp[comp][i] == 0)
      continue;
    int w = i;
    int s = n - i;
    int wow = w * s;
    cur = min(cur, nchoose - wow);
  }
  cout << cur;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  freopen("9.txt", "r", stdin); // freopen(".out","w",stdout);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
/*input
10 39
7 2
7 1
5 6
5 8
9 10
2 8
8 7
3 10
10 1
8 10
2 3
7 4
3 9
4 10
3 4
6 1
6 7
9 5
9 7
6 9
9 4
4 6
7 5
8 3
2 5
9 2
10 7
8 6
8 9
7 3
5 3
4 5
6 3
2 10
5 10
4 2
6 2
8 4
10 6
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back

#define RE(i, n) for (int i = 1; i <= n; i++)
#define RED(i, n) for (int i = n; i > 0; i--)
#define REPS(i, n) for (int i = 1; (i * i) <= n; i++)
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)

#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define print(arr)                                                             \
  for (auto it = arr.begin(); it != arr.end(); ++it)                           \
    cout << *it << " ";                                                        \
  cout << endl;
#define debug(x) cout << x << endl;
#define debug2(x, y) cout << x << " " << y << endl;
#define debug3(x, y, z) cout << x << " " << y << " " << z << endl;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int INF = 1e18 + 1;
const int MOD = 1e9 + 7;
const double PI = 3.14159265358979323846264338;

int raise(int a, int n, int m = MOD) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  int x = 1;
  x *= raise(a, n / 2, m);
  x %= m;
  x *= x;
  x %= m;
  if (n % 2)
    x *= a;
  x %= m;
  return x;
}

int floor1(int n, int k) {
  if (n % k == 0 || n >= 0)
    return n / k;
  return (n / k) - 1;
}

int ceil1(int n, int k) { return floor1(n + k - 1, k); }

const int N = 769;
int mat[N][N];
int n, m;
int ans = 0;
// ans = -comp(l)*comp(r) + L*R
// N-R*R = N*R - R*R
int color[N];
int l[N];
int r[N];
int comp = 0;
bool f = 0;
bool dp[N][N]; // can we have sum j till i th index ?

void bfs(int source) {
  queue<int> q;
  q.push(source);
  color[source] = 1;
  int red = 1;
  int blue = 0;
  int edges = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v = 1; v <= n; v++) {
      if (mat[u][v])
        edges++;
      if (!mat[u][v])
        continue;
      if (color[v] == color[u])
        f = 1;
      if (color[v])
        continue;
      color[v] = (color[u] == 1) ? 2 : 1;
      (color[v] == 1) ? red++ : blue++;
      q.push(v);
    }
  }
  comp++;
  l[comp] = blue;
  r[comp] = red;
}

void solve() {
  cin >> n >> m;
  RE(i, n) {
    color[i] = 0;
    RE(j, n) {
      mat[i][j] = 0;
      dp[i][j] = 0;
    }
  }
  RE(i, m) {
    int a, b;
    cin >> a >> b;
    mat[a][b] = mat[b][a] = 1;
  }
  RE(i, n) {
    RE(j, n) {
      if (i != j and !mat[i][j])
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
    }
  }
  RE(i, n) {
    if (!color[i])
      bfs(i);
  }
  if (f) {
    cout << -1;
    return;
  }
  int mx = 0;
  dp[0][0] = 1;
  for (int i = 1; i <= comp; i++) {
    // cout << l[i] << " " << r[i] << endl;
    for (int j = 0; j <= n; j++) {
      // dp[i][j] = dp[i-1][j];
      if (j >= r[i])
        dp[i][j] |= dp[i - 1][j - r[i]];
      if (j >= l[i])
        dp[i][j] |= dp[i - 1][j - l[i]];
    }
  }
  int cur = INF;
  int nchoose = n * (n - 1);
  nchoose /= 2;
  for (int i = 0; i <= n; i++) {
    if (dp[comp][i] == 0)
      continue;
    int w = i;
    int s = n - i;
    int wow = w * s;
    cur = min(cur, nchoose - wow);
  }
  cout << cur;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}