Problem p01487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <tuple>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)
using ll = long long;
using namespace std;
template <class T> inline void setmax(T &a, T const &b) { a = max(a, b); }

constexpr int max_v = 100000;
int main() {
  // input
  int v, e;
  scanf("%d%d", &v, &e);
  vector<vector<int>> g(v);
  repeat(i, e) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  // solve
  bool is_impossible = false;
  vector<pair<int, int>> component_size;
  {
    vector<char> color(v);
    function<bool(int, char, int &, int &)> go =
        [&](int i, char c, int &red_count, int &black_count) {
          if (color[i])
            return color[i] == c;
          color[i] = c;
          (c == 'R' ? red_count : black_count) += 1;
          char d = c ^ 'R' ^ 'B';
          for (int j : g[i]) {
            if (not go(j, d, red_count, black_count)) {
              return false;
            }
          }
          return true;
        };
    repeat(i, v) if (not color[i]) {
      int red_count = 0;
      int black_count = 0;
      if (go(i, 'R', red_count, black_count)) {
        component_size.emplace_back(red_count, black_count);
      } else {
        is_impossible = true;
        break;
      }
    }
  }
  ll result = -1;
  if (not is_impossible) {
    bitset<max_v + 1> dp = {};
    dp[0] = true;
    for (auto it : component_size) {
      int a, b;
      tie(a, b) = it;
      dp = (dp << a) | (dp << b);
    }
    repeat(a, v + 1) if (dp[a]) {
      ll b = v - a;
      setmax(result, a * b - e);
    }
  }
  // output
  printf("%lld\\n", result);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)
using ll = long long;
using namespace std;
template <class T> inline void setmax(T &a, T const &b) { a = max(a, b); }

int main() {
  // input
  int v, e;
  scanf("%d%d", &v, &e);
  vector<vector<int>> g(v);
  repeat(i, e) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  // solve
  bool is_impossible = false;
  vector<int> component_size_delta;
  {
    vector<char> color(v);
    function<bool(int, char, int &, int &)> go =
        [&](int i, char c, int &red_count, int &black_count) {
          if (color[i])
            return color[i] == c;
          color[i] = c;
          (c == 'R' ? red_count : black_count) += 1;
          char d = c ^ 'R' ^ 'B';
          for (int j : g[i]) {
            if (not go(j, d, red_count, black_count)) {
              return false;
            }
          }
          return true;
        };
    repeat(i, v) if (not color[i]) {
      int red_count = 0;
      int black_count = 0;
      if (go(i, 'R', red_count, black_count)) {
        component_size_delta.push_back(abs(red_count - black_count));
      } else {
        is_impossible = true;
        break;
      }
    }
  }
  ll result = -1;
  if (not is_impossible) {
    vector<int> cur, prv;
    cur.push_back(0);
    for (int delta : component_size_delta) {
      cur.swap(prv);
      cur.clear();
      for (auto it : prv) {
        cur.push_back(it + delta);
        cur.push_back(abs(it - delta));
      }
      whole(sort, cur);
      cur.erase(whole(unique, cur), cur.end());
    }
    for (int delta : cur) {
      ll a = (v + delta) / 2;
      ll b = (v - delta) / 2;
      setmax(result, a * b - e);
    }
  }
  // output
  printf("%lld\\n", result);
  return 0;
}