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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second

using namespace std;

class Pox {
public:
  int rlen, llen, rnext, lnext;
  Pox(int rlen = 0, int llen = 0, int rnext = 0, int lnext = 0)
      : rlen(rlen), llen(llen), rnext(rnext), lnext(lnext) {}
};

int lcm(int a, int b) { return a / __gcd(a, b) * b; }

int rec(vector<Pox> &G, int p) {

  int rcost = !G[p].rnext ? 1 : rec(G, G[p].rnext);
  int lcost = !G[p].lnext ? 1 : rec(G, G[p].lnext);

  int lc = lcm(rcost * G[p].rlen, lcost * G[p].llen);

  return lc / G[p].rlen + lc / G[p].llen;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<Pox> G;
    G.resize(n + 1);
    bool TPO[n + 1];
    for (int i = 0; i < n + 1; i++)
      TPO[i] = false;
    for (int i = 1; i <= n; i++) {
      int a, b, c, d;
      scanf("%d %d %d %d", &a, &b, &c, &d);
      G[i] = Pox(a, b, c, d);
      TPO[c] = TPO[d] = true;
    }

    int st = -1;
    for (int i = 1; i <= n; i++)
      if (!TPO[i]) {
        st = i;
        break;
      }

    cout << rec(G, st) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#define F first
#define S second
#define PPins(a, b, c, d) PP(P(a, b), P(c, d))
#define PPPins(a, b, c, d, e) PPP(a, PP(P(b, c), P(d, e)))
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<int, PP> PPP;

map<int, PP> tree;
int top;

int lcm(int a, int b) { return a / __gcd(a, b) * b; }

int rec(int n) {

  int gcd = __gcd(tree[n].F.F, tree[n].F.S);
  if (!tree[n].S.F && !tree[n].S.S)
    return tree[n].F.F / gcd + tree[n].F.S / gcd;

  if (tree[n].S.F && tree[n].S.S) {
    int Left = rec(tree[n].S.F);
    int Right = rec(tree[n].S.S);

    return 2 * lcm(tree[n].F.F / gcd * Left, tree[n].F.S / gcd * Right);
  }

  if (!tree[n].S.F && tree[n].S.S) {
    int Right = rec(tree[n].S.S);
    int x = lcm((Right * (tree[n].F.S / gcd)), tree[n].F.F / gcd) / tree[n].F.F;
    // cout << "In " << n << " x = " << x << " return = " <<
    // (x*(tree[n].F.F/gcd))/(tree[n].F.S/gcd) + x << endl;
    return (x * (tree[n].F.F / gcd)) / (tree[n].F.S / gcd) + x;
  }

  if (tree[n].S.F && !tree[n].S.S) {
    int Left = rec(tree[n].S.F);
    int x = lcm((Left * (tree[n].F.F / gcd)), tree[n].F.S / gcd) / tree[n].F.S;
    // cout << "In " << n << " x = " << x << " return = " <<
    // (x*(tree[n].F.S/gcd))/(tree[n].F.F/gcd) + x << endl;
    return (x * (tree[n].F.S / gcd)) / (tree[n].F.F / gcd) + x;
  }

  assert(false);
  return 0;
}

int main() {
  int n;
  while (scanf("%d", &n), n) {

    if (!n)
      break;
    tree.clear();
    bool *who = new bool[n + 1];
    for (int i = 0; i < n + 1; i++)
      who[i] = false;
    who[0] = true;
    for (int i = 0; i < n; i++) {
      int p, q, r, b;
      scanf("%d %d %d %d", &p, &q, &r, &b);
      tree[i + 1] = PPins(p, q, r, b);
      who[r] = who[b] = true;
    }

    top = -1;
    for (int i = 1; i < n + 1; i++)
      if (!who[i]) {
        top = i;
        break;
      }
    delete[] who;
    assert(top != -1);

    printf("%d\\n", rec(top));
  }
  return 0;
}