Problem p01462 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int main() {
  for (int n, m; cin >> n >> m && n | m;) {
    double p;
    cin >> p;
    p /= 100;
    vector<pii> es;
    rep(i, m) {
      int u, v;
      cin >> u >> v;
      u--, v--;
      es.emplace_back(u, v);
      es.emplace_back(v, u);
    }

    vd dp(1 << n, 1);
    repi(i, 1, 1 << n) {
      int k = 31 - __builtin_clz(i);
      for (int j = (i - 1) & i; j & 1 << k; j = (j - 1) & i) {
        double q = dp[j];
        for (auto t : es) {
          int u, v;
          tie(u, v) = t;
          if ((j >> u & 1) && ((i - j) >> v & 1))
            q *= p;
        }
        dp[i] -= q;
      }
    }
    printf("%.12f\\n", dp.back());
  }
}

Error: MLE

Fixed submission:

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
  os << '{';
  for (auto i = begin(a); i != end(a); ++i)
    os << (i == begin(a) ? "" : " ") << *i;
  return os << '}';
}

int main() {
  for (int n, m; cin >> n >> m && n | m;) {
    double p;
    cin >> p;
    p *= 0.01;
    vector<pii> es;
    rep(i, m) {
      int u, v;
      cin >> u >> v;
      u--, v--;
      es.emplace_back(u, v);
    }
    sort(all(es));

    map<vector<int>, double> f1; // {連結成分} => 確率
    {
      vector<int> tmp;
      rep(i, n) tmp.push_back(1 << i);
      f1[tmp] = 1;
    }

    for (auto e : es) {
      // dump(f1.size());
      int u, v;
      tie(u, v) = e;
      decltype(f1) f2;
      for (auto t : f1) {
        vector<int> cs = t.first;
        double cp = t.second;
        if (cp < 1e-14)
          continue;
        vector<int> ns;
        int tmp = 0;
        for (auto s : cs) {
          if ((s >> u & 1) || (s >> v & 1))
            tmp |= s;
          else
            ns.push_back(s);
        }
        if (tmp)
          ns.push_back(tmp);
        sort(all(ns));

        f2[ns] += cp * (1 - p);
        f2[cs] += cp * p;
      }
      swap(f1, f2);
    }

    double res = 0;
    for (auto p : f1)
      if (p.first.size() == 1)
        res = p.second;
    printf("%.12f\\n", res);
  }
}