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

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

int N;
vector<int> tree[100];
int dp[100][(1 << 4)];

int m;
string str, tmp;
int p, len;

int ans, cnt;
int t[10];

void make_tree() {
  int k = N++;
  if (str[p] == '(') {
    p++;
    tree[k].push_back(N);
    make_tree();
    p++;
    tree[k].push_back(N);
    make_tree();
    p++;
  } else {
    int num = str[p] - '0';
    p++;
    dp[k][t[num]] = 1;
  }
}

int input() {

  for (int i = 0; i < 100; i++) {
    tree[i].clear();
    for (int j = 0; j < (1 << 4); j++)
      dp[i][j] = 0;
  }

  getline(cin, str);
  if (str == "END")
    return 0;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    t[i] = 0;
    int a;
    for (int j = 0; j < 4; j++) {
      cin >> a;
      t[i] = t[i] * 2 + a;
    }
  }
  getline(cin, tmp);
  N = 0;
  p = 0;
  len = str.size();
  make_tree();
  return 1;
}

void dfs(int pos) {
  if (tree[pos].size() > 0) {
    int lc = tree[pos][0];
    int rc = tree[pos][1];
    dfs(lc);
    dfs(rc);
    for (int L = 0; L < (1 << 4); L++) {
      for (int R = 0; R < (1 << 4); R++) {
        dp[pos][L | R] += dp[lc][L] * dp[rc][R];
        dp[pos][L & R] += dp[lc][L] * dp[rc][R];
        dp[pos][L ^ R] += dp[lc][L] * dp[rc][R];
      }
    }
  }
}

int main() {
  while (input()) {
    ans = 0;
    cnt = 0;
    dfs(0);
    cout << dp[0][15] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cassert>
#include <cstdio>
#include <iostream>
using namespace std;
struct node {
  int bit, mark;
};
node tree[10024];
int m;
void out(void);

int ans;
int check(int ima) {
  if (tree[ima * 2 + 1].mark == -1 && tree[ima * 2 + 2].mark == -1)
    return tree[ima].bit;
  int mark = tree[ima].mark;
  int l = check(ima * 2 + 1);
  int r = check(ima * 2 + 2);
  if (mark == 0)
    return l | r;
  if (mark == 1)
    return l & r;
  if (mark == 2)
    return l ^ r;
}

int cnt = 0;
void mark_dfs(int ima) {
  while ((tree[ima].mark == -1 || tree[ima * 2 + 1].mark == -1 ||
          tree[ima * 2 + 2].mark == -1) &&
         ima <= 10000)
    ima++;
  if (ima > 10000) {
    cnt++;
    if (check(0) == (1 << 4) - 1)
      ans++;
  } else
    for (int i = 0; i <= 2; i++)
      tree[ima].mark = i, mark_dfs(ima + 1);
}

int input() {
  char ch;
  for (int i = 0; i < 10024; i++)
    tree[i].bit = tree[i].mark = -1;
  int ima = 0, num = 0, p[10024] = {}, flg = 1;
  while (1) {
    scanf("%c", &ch);
    tree[ima].mark = 0;
    if (ch == 'E')
      flg = 0;
    if (ch == '\\n')
      break;
    if (ch == '(')
      ima = ima * 2 + 1;
    else if ('0' <= ch && ch <= '9')
      num = num * 10 + ch - '0';
    else if (ch == ' ')
      p[num] = ima, num = 0, ima++;
    else if (ch == ')')
      p[num] = ima, num = 0, ima = (ima - 1) / 2;
  }
  if (flg == 0)
    return flg;

  cin >> m;
  for (int i = 1; i <= m; i++) {
    int bit = 0, a;
    for (int j = 0; j < 4; j++)
      cin >> a, bit += a << (3 - j);
    tree[p[i]].bit = bit;
  }
  scanf("%c", &ch);
  return flg;
}

int main() {
  while (input()) {
    ans = 0;
    cnt = 0;
    mark_dfs(0);
    int sum = 1;
    for (int i = 1; i < m; i++)
      sum *= 3;
    assert(cnt == sum);
    cout << ans << endl;
  }
  return 0;
}

void out() {
  int a = 0, b = 2;
  for (int i = 0; i < 7; i++) {
    cout << tree[i].mark << " ";
    if (a == i)
      cout << endl, a += b, b *= 2;
  }
  cout << endl;
}