Problem p01487 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long lint;
typedef pair<int, int> pii;

int N, M, dp[100010];
vector<vector<int>> adj;
vector<int> g[2], col;
map<int, int> part_cnt;

bool dfs(int v, int c) {
  if (col[v] != -1) {
    if (col[v] != c)
      return false;
    return true;
  }

  col[v] = c;
  g[c].push_back(v);

  for (int u : adj[v])
    if (!dfs(u, 1 - c))
      return false;

  return true;
}

lint calc(lint A) {
  memset(dp, -1, sizeof(dp));

  dp[0] = 0;

  for (auto it = part_cnt.begin(); it != part_cnt.end(); ++it) {
    int a = (*it).first, c = (*it).second;

    for_(j, 0, N + 1) {
      if (dp[j] >= 0) {
        dp[j] = c;
      } else if (j < a || dp[j - a] <= 0) {
        dp[j] = -1;
      } else {
        dp[j] = dp[j - a] - 1;
      }
    }
  }

  lint res = 0;

  for_(a, 0, N + 1) {
    if (dp[a] == -1)
      continue;
    lint B = N - (A + a);
    res = max(res, (A + a) * B);
  }

  return res;
}

void solve() {
  col.assign(N, -1);

  bool flag = true;
  lint A = 0;

  for_(v, 0, N) {
    if (col[v] != -1)
      continue;
    for_(i, 0, 2) g[i].clear();

    flag &= dfs(v, 0);

    int a = min(g[0].size(), g[1].size()), b = max(g[0].size(), g[1].size());
    ++part_cnt[b - a];
    A += a;
  }

  cout << (!flag ? -1 : calc(A) - M) << endl;
}

int main() {
  cin >> N >> M;

  adj.assign(N, vector<int>());

  for_(i, 0, M) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long lint;
typedef pair<int, int> pii;

int N, M, dp[100010];
vector<vector<int>> adj;
vector<int> g[2], col;
map<pii, int> part_cnt;

bool dfs(int v, int c) {
  if (col[v] != -1) {
    if (col[v] != c)
      return false;
    return true;
  }

  col[v] = c;
  g[c].push_back(v);

  for (int u : adj[v])
    if (!dfs(u, 1 - c))
      return false;

  return true;
}

lint calc() {
  memset(dp, -1, sizeof(dp));

  dp[0] = 0;

  lint A = 0;

  for (auto it = part_cnt.begin(); it != part_cnt.end(); ++it) {
    pii p = (*it).first;
    int a = p.second, c = (*it).second;
    A += p.first;

    for_(j, 0, 2 * N) {
      if (dp[j] >= 0) {
        dp[j] = c;
      } else if (j < a || dp[j - a] <= 0) {
        dp[j] = -1;
      } else {
        dp[j] = dp[j - a] - 1;
      }
    }
  }

  lint res = 0;

  for_(a, 0, 2 * N) {
    if (dp[a] == -1)
      continue;
    lint B = N - (A + a);
    res = max(res, (A + a) * B);
  }

  return res;
}

void solve() {
  col.assign(N, -1);

  lint sub_sum = 0;
  bool flag = true;

  for_(v, 0, N) {
    if (col[v] != -1)
      continue;
    for_(i, 0, 2) g[i].clear();

    flag &= dfs(v, 0);

    int a = min(g[0].size(), g[1].size()), b = max(g[0].size(), g[1].size());
    ++part_cnt[pii(a, b - a)];

    for (int u : g[0])
      sub_sum += adj[u].size();
  }

  cout << (!flag ? -1 : calc() - sub_sum) << endl;
}

int main() {
  cin >> N >> M;

  adj.assign(N, vector<int>());

  for_(i, 0, M) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  solve();
}