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

#include <iostream>
#include <vector>
using namespace std;

long long N, M, dpa[1 << 18], dpb[1 << 18], power[200], powerinv[200],
    power3[200], mod = 1000000007;
bool used[16][16];
int V3[16];

vector<long long> solve(int pos) {
  vector<long long> dp((1 << N), 0);
  for (int i = 0; i < (1 << N); i++) {
    int bit[15];
    for (int j = 0; j < N; j++)
      bit[j] = (i / (1 << j)) % 2;
    if (bit[pos] == 0)
      continue;

    int cnt = 0;
    for (int j = 0; j < N; j++) {
      for (int k = j + 1; k < N; k++) {
        if (bit[j] == 1 && bit[k] == 1 && used[j][k] == true)
          cnt++;
      }
    }
    long long res = power[cnt];
    int G = 0;
    vector<int> V1;
    for (int j = 0; j < N; j++) {
      if (bit[j] == 1) {
        if (j == pos)
          G = (1 << V1.size());
        V1.push_back(j);
      }
    }

    for (int j = 0; j < (1 << V1.size()); j++) {
      if ((j & G) == 0)
        continue;
      int R = 0, T = 0;
      for (int k = 0; k < V1.size(); k++) {
        if ((j & (1 << k)) != 0)
          R += (1 << V1[k]);
        else {
          V3[T] = V1[k];
          T++;
        }
      }
      long long Y = dp[R];
      if (Y == 0)
        continue;
      for (int k = 0; k < T; k++) {
        for (int l = k + 1; l < T; l++) {
          if (used[V3[k]][V3[l]] == true) {
            Y *= 2;
            if (Y >= mod)
              Y -= mod;
          }
        }
      }
      res -= Y;
      if (res < 0)
        res += mod;
    }
    dp[i] = res;
  }
  return dp;
}

int main() {
  power[0] = 1;
  for (int i = 1; i < 200; i++)
    power[i] = (2LL * power[i - 1]) % mod;
  powerinv[0] = 1;
  for (int i = 1; i < 200; i++)
    powerinv[i] = (500000004LL * powerinv[i - 1]) % mod;
  power3[0] = 1;
  for (int i = 1; i < 200; i++)
    power3[i] = (3LL * power3[i - 1]) % mod;

  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    used[a][b] = true;
    used[b][a] = true;
  }

  vector<long long> A1 = solve(0);
  for (int i = 0; i < (1 << N); i++)
    dpa[i] = A1[i];
  vector<long long> A2 = solve(1);
  for (int i = 0; i < (1 << N); i++)
    dpb[i] = A2[i];

  long long ret = 0;
  for (int i = 7; i < power3[N]; i += 9) {
    int col[15];
    for (int j = 0; j < N; j++)
      col[j] = (i / power3[j]) % 3;
    int pa = 0, pb = 0;
    for (int j = 0; j < N; j++) {
      if (col[j] == 1)
        pa += (1 << j);
      if (col[j] == 2)
        pb += (1 << j);
    }

    if (dpa[pa] == 0 || dpb[pb] == 0)
      continue;
    long long E1 = dpa[pa] * dpb[pb] % mod;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        if (col[j] == 1 && col[k] == 2 && used[j][k] == true)
          E1 *= 0;
        if (col[j] == 0 && col[k] == 0 && j < k && used[j][k] == true) {
          E1 *= 2;
          E1 %= mod;
        }
      }
    }
    ret += E1;
    ret %= mod;
  }
  cout << (power[M] - ret + mod) % mod << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

long long N, M, dpa[1 << 18], dpb[1 << 18], power[200], powerinv[200],
    power3[200], mod = 1000000007;
bool used[16][16];
int V3[16];

vector<long long> solve(int pos) {
  vector<long long> dp((1 << N), 0);
  for (int i = 0; i < (1 << N); i++) {
    int bit[15];
    for (int j = 0; j < N; j++)
      bit[j] = (i / (1 << j)) % 2;
    if (bit[pos] == 0)
      continue;

    int cnt = 0;
    for (int j = 0; j < N; j++) {
      for (int k = j + 1; k < N; k++) {
        if (bit[j] == 1 && bit[k] == 1 && used[j][k] == true)
          cnt++;
      }
    }
    long long res = power[cnt];
    vector<int> V1;
    for (int j = 0; j < N; j++) {
      if (bit[j] == 1)
        V1.push_back(j);
    }

    for (int j = 0; j < (1 << V1.size()); j++) {
      int R = 0, T = 0;
      for (int k = 0; k < V1.size(); k++) {
        if ((j & (1 << k)) != 0)
          R += (1 << V1[k]);
        else {
          V3[T] = V1[k];
          T++;
        }
      }
      long long Y = dp[R];
      if (Y == 0)
        continue;
      for (int k = 0; k < T; k++) {
        for (int l = k + 1; l < T; l++) {
          if (used[V3[k]][V3[l]] == true) {
            Y *= 2;
            if (Y >= mod)
              Y -= mod;
          }
        }
      }
      res -= Y;
      if (res < 0)
        res += mod;
    }
    dp[i] = res;
  }
  return dp;
}

int main() {
  power[0] = 1;
  for (int i = 1; i < 200; i++)
    power[i] = (2LL * power[i - 1]) % mod;
  powerinv[0] = 1;
  for (int i = 1; i < 200; i++)
    powerinv[i] = (500000004LL * powerinv[i - 1]) % mod;
  power3[0] = 1;
  for (int i = 1; i < 200; i++)
    power3[i] = (3LL * power3[i - 1]) % mod;

  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    used[a][b] = true;
    used[b][a] = true;
  }

  vector<long long> A1 = solve(0);
  for (int i = 0; i < (1 << N); i++)
    dpa[i] = A1[i];
  vector<long long> A2 = solve(1);
  for (int i = 0; i < (1 << N); i++)
    dpb[i] = A2[i];

  long long ret = 0;
  for (int i = 7; i < power3[N]; i += 9) {
    int col[15];
    for (int j = 0; j < N; j++)
      col[j] = (i / power3[j]) % 3;
    int pa = 0, pb = 0;
    for (int j = 0; j < N; j++) {
      if (col[j] == 1)
        pa += (1 << j);
      if (col[j] == 2)
        pb += (1 << j);
    }

    if (dpa[pa] == 0 || dpb[pb] == 0)
      continue;
    long long E1 = dpa[pa] * dpb[pb] % mod;
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        if (col[j] == 1 && col[k] == 2 && used[j][k] == true)
          E1 *= 0;
        if (col[j] == 0 && col[k] == 0 && j < k && used[j][k] == true) {
          E1 *= 2;
          E1 %= mod;
        }
      }
    }
    ret += E1;
    ret %= mod;
  }
  cout << (power[M] - ret + mod) % mod << endl;
  return 0;
}