Problem p03321 (C++)

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

#define int long long

using namespace std;

int adj[800][800];

vector<int> a[805];
int level[805];
int vis[805];

int odd = 0;
int even = 0;

void dfs(int n, int cur) {
  if (cur % 2)
    odd++;
  else
    even++;
  level[n] = cur;
  vis[n] = 1;

  for (int i = 0; i < a[n].size(); i++) {
    if (vis[a[n][i]] == 0)
      dfs(a[n][i], cur + 1);
  }
}

int32_t main() {
  int n, m;
  cin >> n >> m;

  if (m == (n * (n - 1) / 2)) {
    while (1) {
      ;
    }
    cout << m << endl;
    return 0;
  }

  int t1, t2;
  for (int i = 0; i < m; i++) {
    cin >> t1 >> t2;
    adj[t1][t2] = 1;
    adj[t2][t1] = 1;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      if (adj[i][j] == 0)
        a[i].push_back(j);
    }
  }

  vector<pair<int, int>> arr;

  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      odd = 0;
      even = 0;
      dfs(i, 1);
      arr.push_back({odd, even});
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < a[i].size(); j++) {
      int temp = level[i] - level[a[i][j]];
      if (temp < 0)
        temp *= -1;
      if (temp % 2 == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  int dp[705][705] = {0};

  dp[0][arr[0].first] = 1;
  dp[0][arr[0].second] = 1;

  for (int i = 1; i < arr.size(); i++) {
    for (int j = 0; j <= 700; j++) {
      if (dp[i - 1][j] == 1) {
        dp[i][j + arr[i].first] = 1;
        dp[i][j + arr[i].second] = 1;
      }
    }
  }

  int cur = arr.size() - 1;
  int ans = m;
  for (int i = 1; i <= n; i++) {
    if (dp[cur][i] == 1) {
      int temp1 = i * (i - 1) / 2;
      int temp2 = (n - i) * (n - i - 1) / 2;
      ans = min(ans, temp1 + temp2);
    }
  }
  cout << ans << endl;

  return 0;
}


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

#define int long long

using namespace std;

int adj[800][800];

vector<int> a[805];
int level[805];
int vis[805];

int odd = 0;
int even = 0;

void dfs(int n, int cur) {
  if (cur % 2)
    odd++;
  else
    even++;
  level[n] = cur;
  vis[n] = 1;

  for (int i = 0; i < a[n].size(); i++) {
    if (vis[a[n][i]] == 0)
      dfs(a[n][i], cur + 1);
  }
}

int32_t main() {
  int n, m;
  cin >> n >> m;

  int t1, t2;
  for (int i = 0; i < m; i++) {
    cin >> t1 >> t2;
    adj[t1][t2] = 1;
    adj[t2][t1] = 1;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      if (adj[i][j] == 0)
        a[i].push_back(j);
    }
  }

  vector<pair<int, int>> arr;

  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      odd = 0;
      even = 0;
      dfs(i, 1);
      arr.push_back({odd, even});
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < a[i].size(); j++) {
      int temp = level[i] - level[a[i][j]];
      if (temp < 0)
        temp *= -1;
      if (temp % 2 == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  int dp[705][705] = {0};

  dp[0][arr[0].first] = 1;
  dp[0][arr[0].second] = 1;

  for (int i = 1; i < arr.size(); i++) {
    for (int j = 0; j <= 700; j++) {
      if (dp[i - 1][j] == 1) {
        dp[i][j + arr[i].first] = 1;
        dp[i][j + arr[i].second] = 1;
      }
    }
  }

  int cur = arr.size() - 1;
  int ans = m;
  for (int i = 1; i <= n; i++) {
    if (dp[cur][i] == 1) {
      int temp1 = i * (i - 1) / 2;
      int temp2 = (n - i) * (n - i - 1) / 2;
      ans = min(ans, temp1 + temp2);
    }
  }
  cout << ans << endl;

  return 0;
}