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

#include <bits/stdc++.h>
using namespace std;
int V, E;
double P, Q;
vector<int> G[15];

void printbit(int S) {
  for (int i = 0; i < V; i++) {
    cout << (S >> i & 1) << ' ';
  }
  cout << endl;
}

double func(int S, int U) {

  double res = 1;
  for (int i = 0; i < V; i++) {
    if (S >> i & 1) {
      for (int j = 0; j < (int)G[i].size(); j++) {
        int w = G[i][j];
        if (S >> w & 1)
          continue;
        if (U >> w & 1)
          res *= P;
      }
    }
  }

  return res;
}

double dp[(1 << 14)];

double rec(int S) {

  if (dp[S] != -1)
    return dp[S];
  if (S == 1)
    return 1.0;

  double res = 0;
  int sup = S;
  int sub = sup;

  do {
    if (sub & 1) {
      if (sub != S) {
        double A = rec(sub);
        double B = func(sub, S);
        res += A * B;
      }
    }
    sub = (sub - 1) & sup;
  } while (sub != sup);

  res = 1.0 - res;

  return dp[S] = res;
}

void init() {
  for (int i = 0; i < 15; i++)
    G[i].clear();
  for (int i = 0; i < (1 << 14); i++) {
    dp[i] = -1;
  }
}

bool check() {
  bool visited[15] = {};
  visited[0] = true;
  queue<int> que;
  que.push(0);

  while (!que.empty()) {
    int pos = que.front();
    que.pop();
    for (int i = 0; i < (int)G[pos].size(); i++) {
      int w = G[pos][i];
      if (visited[w])
        continue;
      visited[w] = true;
      que.push(w);
    }
  }

  for (int i = 0; i < V; i++)
    if (!visited[i])
      return false;

  return true;
}

int main() {
  while (cin >> V >> E >> P) {
    init();
    P /= 100.0;
    Q = 1.0 - P;
    for (int i = 0; i < E; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }

    if (!check()) {
      printf("0.0\\n");
      continue;
    }
    printf("%.10f\\n", rec((1 << V) - 1));
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int V, E;
double P, Q;
vector<int> G[15];

void printbit(int S) {
  for (int i = 0; i < V; i++) {
    cout << (S >> i & 1) << ' ';
  }
  cout << endl;
}

double func(int S, int U) {

  double res = 1;
  for (int i = 0; i < V; i++) {
    if (S >> i & 1) {
      for (int j = 0; j < (int)G[i].size(); j++) {
        int w = G[i][j];
        if (S >> w & 1)
          continue;
        if (U >> w & 1)
          res *= P;
      }
    }
  }

  return res;
}

double dp[(1 << 14)];

double rec(int S) {

  if (dp[S] != -1)
    return dp[S];
  if (S == 1)
    return 1.0;

  double res = 0;
  int sup = S;
  int sub = sup;

  do {
    if (sub & 1) {
      if (sub != S) {
        double A = rec(sub);
        double B = func(sub, S);
        res += A * B;
      }
    }
    sub = (sub - 1);
  } while (sub != sup);

  res = 1.0 - res;

  return dp[S] = res;
}

void init() {
  for (int i = 0; i < 15; i++)
    G[i].clear();
  for (int i = 0; i < (1 << 14); i++) {
    dp[i] = -1;
  }
}

bool check() {
  bool visited[15] = {};
  visited[0] = true;
  queue<int> que;
  que.push(0);

  while (!que.empty()) {
    int pos = que.front();
    que.pop();
    for (int i = 0; i < (int)G[pos].size(); i++) {
      int w = G[pos][i];
      if (visited[w])
        continue;
      visited[w] = true;
      que.push(w);
    }
  }

  for (int i = 0; i < V; i++)
    if (!visited[i])
      return false;

  return true;
}

int main() {
  while (cin >> V >> E >> P) {
    init();
    P /= 100.0;
    Q = 1.0 - P;
    for (int i = 0; i < E; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      G[a].push_back(b);
      G[b].push_back(a);
    }

    if (!check()) {
      printf("0.0\\n");
      continue;
    }
    printf("%.10f\\n", rec((1 << V) - 1));
  }
  return 0;
}