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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <vector>

using namespace std;

enum { Leaf, Node };

struct Stage {
  int type;
  int required_vote;

  union {
    int n;
    vector<Stage *> *childs;
  } u;
};

bool comp(const Stage *l, const Stage *r) {
  return l->required_vote < r->required_vote;
}

char S[20000];

Stage *parse(char *s, char **next) {
  assert(s[0] == '[');
  if (isdigit(s[1])) {
    int n = 0;

    int i = 1;
    for (; isdigit(s[i]); i++) {
      n *= 10;
      n += (int)(s[i] - '0');
    }
    assert(s[i] == ']');

    *next = &s[i + 1];

    Stage *r = new Stage;
    r->type = Leaf;
    r->u.n = n;

    return r;
  } else if (s[1] == '[') {
    ++s;

    Stage *r = new Stage;
    r->type = Node;
    r->u.childs = new vector<Stage *>;

    vector<Stage *> &childs = *(r->u.childs);

    for (; *s != ']';) {
      assert(*s == '[');

      char *n;
      Stage *c = parse(s, &n);
      childs.push_back(c);

      s = n;
    }

    *next = s + 1;

    return r;
  }

  assert(false);
  return 0;
}

int more_than_half(int i) { return i / 2 + 1; }

void calc(Stage *s) {
  if (s->type == Leaf) {
    s->required_vote = more_than_half(s->u.n);
  } else if (s->type == Node) {
    vector<Stage *> &v = *(s->u.childs);

    for (int i = 0; i < (int)v.size(); i++) {
      calc(v[i]);
    }

    sort(v.begin(), v.end(), comp);

    int r = 0;
    for (int i = 0; i < more_than_half(v.size()); i++) {
      r += v[i]->required_vote;
    }

    s->required_vote = r;
  }
}

void cleanup(Stage *s) {
  if (s->type == Leaf) {
    delete s;
  } else if (s->type == Node) {
    vector<Stage *> &v = *(s->u.childs);

    for (int i = 0; i < (int)v.size(); i++) {
      cleanup(v[i]);
    }

    delete s->u.childs;
    delete s;
  }
}

int main() {
  int n;
  char ns[100];
  fgets(ns, 99, stdin);
  n = atoi(ns);

  for (int zz = 0; zz < n; zz++) {
    for (int i = 0; i < 10002; i++) {
      S[i] = 0;
    }
    fgets(S, 20000, stdin);
    char *dummy;
    Stage *node = parse(S, &dummy);

    calc(node);

    printf("%d\\n", node->required_vote);

    cleanup(node);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <vector>

using namespace std;

enum { Leaf, Node };

struct Stage {
  int type;
  int required_vote;

  union {
    int n;
    vector<Stage *> *childs;
  } u;
};

bool comp(const Stage *l, const Stage *r) {
  return l->required_vote < r->required_vote;
}

char S[10002];

Stage *parse(char *s, char **next) {
  assert(s[0] == '[');
  if (isdigit(s[1])) {
    int n = 0;

    int i = 1;
    for (; isdigit(s[i]); i++) {
      n *= 10;
      n += (int)(s[i] - '0');
    }
    assert(s[i] == ']');

    *next = &s[i + 1];

    Stage *r = new Stage;
    r->type = Leaf;
    r->u.n = n;

    return r;
  } else if (s[1] == '[') {
    ++s;

    Stage *r = new Stage;
    r->type = Node;
    r->u.childs = new vector<Stage *>;

    vector<Stage *> &childs = *(r->u.childs);

    for (; *s != ']';) {
      assert(*s == '[');

      char *n;
      Stage *c = parse(s, &n);
      childs.push_back(c);

      s = n;
    }

    *next = s + 1;

    return r;
  }

  assert(false);
  return 0;
}

int more_than_half(int i) { return i / 2 + 1; }

void calc(Stage *s) {
  if (s->type == Leaf) {
    s->required_vote = more_than_half(s->u.n);
  } else if (s->type == Node) {
    vector<Stage *> &v = *(s->u.childs);

    for (int i = 0; i < (int)v.size(); i++) {
      calc(v[i]);
    }

    sort(v.begin(), v.end(), comp);

    int r = 0;
    for (int i = 0; i < more_than_half(v.size()); i++) {
      r += v[i]->required_vote;
    }

    s->required_vote = r;
  }
}

void cleanup(Stage *s) {
  if (s->type == Leaf) {
    delete s;
  } else if (s->type == Node) {
    vector<Stage *> &v = *(s->u.childs);

    for (int i = 0; i < (int)v.size(); i++) {
      cleanup(v[i]);
    }

    delete s->u.childs;
    delete s;
  }
}

int main() {
  int n;
  char ns[100];
  fgets(ns, 99, stdin);
  n = atoi(ns);

  for (int zz = 0; zz < n; zz++) {
    fgets(S, 10001, stdin);
    char *dummy;
    Stage *node = parse(S, &dummy);

    calc(node);

    printf("%d\\n", node->required_vote);

    cleanup(node);
  }
  return 0;
}