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

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
struct Bar {
  int p, q, r, b;
  Bar(int _p, int _q, int _r, int _b) { p = _p, q = _q, r = _r, b = _b; }
};
struct Fraction {
  int bunbo, bunshi;
  Fraction(int _bunbo, int _bunshi) {
    int G = gcd(_bunbo, _bunshi);
    bunbo = _bunbo / G, bunshi = _bunshi / G;
  }
};
int n;
vector<Bar> Bars;
vector<Fraction> Fractions;
bool child[100];
void solve(int X, Fraction C) {
  if (Bars[X].r > 0)
    solve(Bars[X].r - 1,
          Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].q));
  else
    Fractions.push_back(
        Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].q));
  if (Bars[X].b > 0)
    solve(Bars[X].b - 1,
          Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].p));
  else
    Fractions.push_back(
        Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].p));
}
int main() {
  while (scanf("%d", &n), n) {
    Bars.clear();
    Fractions.clear();
    memset(child, 0, sizeof(child));
    for (int i = 0; i < n; i++) {
      int p, q, r, b;
      scanf("%d %d %d %d", &p, &q, &r, &b);
      if (r > 0)
        child[r - 1] = true;
      if (b > 0)
        child[b - 1] = true;
      Bars.push_back(Bar(p, q, r, b));
    }
    int par = 0;
    for (int i = 0; i < n; i++) {
      if (!child[i]) {
        par = i;
        break;
      }
    }
    solve(par, Fraction(1, 1));
    int L = 1;
    for (int i = 0; i < Fractions.size(); i++) {
      L = lcm(L, Fractions[i].bunbo);
    }
    printf("%d\\n", L);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct Bar {
  int p, q, r, b;
  Bar(int _p, int _q, int _r, int _b) { p = _p, q = _q, r = _r, b = _b; }
};
struct Fraction {
  int bunbo, bunshi;
  Fraction(int _bunbo, int _bunshi) { bunbo = _bunbo, bunshi = _bunshi; }
};
int n;
vector<Bar> Bars;
vector<Fraction> Fractions;
bool child[100];
void solve(int X, Fraction C) {
  if (Bars[X].r > 0)
    solve(Bars[X].r - 1,
          Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].q));
  else
    Fractions.push_back(
        Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].q));
  if (Bars[X].b > 0)
    solve(Bars[X].b - 1,
          Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].p));
  else
    Fractions.push_back(
        Fraction(C.bunbo * (Bars[X].p + Bars[X].q), C.bunshi * Bars[X].p));
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int main() {
  while (scanf("%d", &n), n) {
    Bars.clear();
    Fractions.clear();
    memset(child, 0, sizeof(child));
    for (int i = 0; i < n; i++) {
      int p, q, r, b;
      scanf("%d %d %d %d", &p, &q, &r, &b);
      if (r > 0)
        child[r - 1] = true;
      if (b > 0)
        child[b - 1] = true;
      Bars.push_back(Bar(p, q, r, b));
    }
    int par = 0;
    for (int i = 0; i < n; i++) {
      if (!child[i]) {
        par = i;
        break;
      }
    }
    solve(par, Fraction(1, 1));
    int L = 1;
    for (int i = 0; i < Fractions.size(); i++) {
      int G = gcd(Fractions[i].bunbo, Fractions[i].bunshi);
      Fractions[i].bunbo /= G;
      L = lcm(L, Fractions[i].bunbo);
    }
    printf("%d\\n", L);
  }
}