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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 18;
int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define Se second
#define Fi first

const int DEB = 0;
const int MX = DEB ? 1 << 5 : 1 << 15;
const int MM = 1000000007;

int A[MX], B[MX];

int G[15][15];
int tmp[MX];
int pw2[MX];
int N, M;

void solve(int c, int A[MX]) {
  for (int t = 0; t < MX; t++) {
    if (~t & c)
      continue;
    int cnt = tmp[t];
    A[t] = pw2[cnt];
    for (int u = t; u; u = (u - 1) & t) {
      if (~u & c || u == t)
        continue;
      int v = t ^ u, cnt = tmp[v];
      A[t] = (A[t] - (ll)pw2[cnt] * A[u] % MM + MM) % MM;
    }
  }
}

int main() {
  pw2[0] = 1;
  for (int i = 1; i < MX; i++)
    pw2[i] = pw2[i - 1] * 2 % MM;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    G[a][b] = 1;
  }
  if (G[0][1] || G[1][0])
    return !printf("%d\\n", pw2[M]);
  for (int m = 1; m < MX; m++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (m & 1 << i && m & 1 << j)
          tmp[m] += G[i][j];
      }
    }
  }
  solve(1, A);
  solve(2, B);

  ll ans = 0;
  for (int i = 0; i < MX; i++) {
    for (int j = i; j; j = (j - 1) & i) {
      int p = j, q = i ^ j, r = (MX - 1) ^ i;
      if (~p & 1 || ~q & 2)
        continue;
      int cnt = 0, tp = 1;
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          if (r & 1 << i && r & 1 << j)
            cnt += G[i][j];
          if (p & 1 << i && q & 1 << j && (G[i][j] || G[j][i]))
            tp = 0;
        }
      }
      ans = (ans + (ll)tp * pw2[cnt] * A[p] % MM * B[q]) % MM;
    }
  }
  printf("%lld\\n", (pw2[M] - ans + MM) % MM);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

int IT_MAX = 1 << 18;
int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define Se second
#define Fi first

const int DEB = 0;
const int MX = DEB ? 1 << 5 : 1 << 15;
const int MM = 1000000007;

int A[MX], B[MX];

int G[15][15];
int pw2[MX];
int N, M;

void solve(int c, int A[MX]) {
  for (int t = 0; t < MX; t++) {
    if (~t & c)
      continue;
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (t & 1 << i && t & 1 << j)
          cnt += G[i][j];
      }
    }
    A[t] = pw2[cnt];
    for (int u = t; u; u = (u - 1) & t) {
      if (~u & c || u == t)
        continue;
      int v = t ^ u, cnt = 0;
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          if (v & 1 << i && v & 1 << j)
            cnt += G[i][j];
        }
      }
      A[t] = (A[t] - (ll)pw2[cnt] * A[u] % MM + MM) % MM;
    }
  }
}

int main() {
  pw2[0] = 1;
  for (int i = 1; i < MX; i++)
    pw2[i] = pw2[i - 1] * 2 % MM;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    G[a][b] = 1;
  }
  if (G[0][1] || G[1][0])
    return !printf("%d\\n", pw2[M]);
  solve(1, A);
  solve(2, B);

  ll ans = 0;
  for (int i = 0; i < MX; i++) {
    for (int j = i; j; j = (j - 1) & i) {
      int p = j, q = i ^ j, r = (MX - 1) ^ i;
      if (~p & 1 || ~q & 2)
        continue;
      int cnt = 0, tp = 1;
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          if (r & 1 << i && r & 1 << j)
            cnt += G[i][j];
          if (p & 1 << i && q & 1 << j && (G[i][j] || G[j][i]))
            tp = 0;
        }
      }
      ans = (ans + (ll)tp * pw2[cnt] * A[p] % MM * B[q]) % MM;
    }
  }
  printf("%lld\\n", (pw2[M] - ans + MM) % MM);
}