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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

long long int gcd(long long int a, long long int b) {
  long long int tmp;
  while (b != 0ll) {
    a -= b * (long long int)(a / b);
    tmp = a;
    a = b;
    b = tmp;
  }
  return a;
}

int lcm(int a, int b) {
  long long int la = (long long int)a, lb = (long long int)b;
  return la / gcd(la, lb) * lb;
}

struct bou {
  int left, right;
  int lbou, rbou;
};

int main() {
  int n, res = 1;
  vector<bou> tree;
  int si[100], bo[100], par[100];
  while (cin >> n, n) {
    tree.clear();
    res = 1;
    for (int i = 0; i < n; i++) {
      par[i] = -1;
      si[i] = 1;
      bo[i] = 1;
    }
    for (int i = 0; i < n; i++) {
      long long int a, b;
      int c, d;
      cin >> a >> b >> c >> d;
      tree.push_back({a / gcd(a, b), b / gcd(a, b), c - 1, d - 1});
      if (c >= 1)
        par[c - 1] = i;
      if (d >= 1)
        par[d - 1] = i;
    }
    queue<int> que;
    for (int i = 0; i < n; i++)
      if (par[i] == -1)
        que.push(i);
    while (!que.empty()) {
      int q = que.front();
      que.pop();
      // cout << q;
      bou now = tree[q];
      long long int bun[4];
      bun[0] = si[q] * now.right;
      bun[1] = bo[q] * (now.left + now.right);
      bun[2] = si[q] * now.left;
      bun[3] = bun[1];
      int g1 = gcd(bun[0], bun[1]), g2 = gcd(bun[2], bun[3]);
      bun[0] /= g1;
      bun[1] /= g1;
      bun[2] /= g2;
      bun[3] /= g2;
      if (now.lbou != -1) {
        si[now.lbou] = (int)bun[0];
        bo[now.lbou] = (int)bun[1];
        que.push(now.lbou);
      } else {
        res = lcm(res, (int)bun[1]);
      }
      // cout << "k" << endl;
      if (now.rbou != -1) {
        si[now.rbou] = (int)bun[2];
        bo[now.rbou] = (int)bun[3];
        que.push(now.rbou);
      } else {
        res = lcm(res, (int)bun[3]);
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int gcd(int a, int b) {
  int tmp;
  while (b != 0) {
    a -= b * (int)(a / b);
    tmp = a;
    a = b;
    b = tmp;
  }
  return a;
}

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

struct bou {
  int left, right;
  int lbou, rbou;
};

int main() {
  int n, res = 1;
  vector<bou> tree;
  int si[100], bo[100], par[100];
  while (cin >> n, n) {
    tree.clear();
    res = 1;
    for (int i = 0; i < n; i++)
      par[i] = -1;
    for (int i = 0; i < n; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      tree.push_back({a / gcd(a, b), b / gcd(a, b), c - 1, d - 1});
      if (c >= 1)
        par[c - 1] = i;
      if (d >= 1)
        par[d - 1] = i;
    }
    queue<int> que;
    for (int i = 0; i < n; i++)
      if (par[i] == -1)
        que.push(i);
    si[que.front()] = 1;
    bo[que.front()] = 1;
    while (!que.empty()) {
      int q = que.front();
      que.pop();
      bou now = tree[q];
      int bun[4];
      bun[0] = si[q] * now.left;
      bun[1] = bo[q] * (now.left + now.right);
      bun[2] = si[q] * now.right;
      bun[3] = bun[1];
      bun[0] /= gcd(bun[0], bun[1]);
      bun[1] /= gcd(bun[0], bun[1]);
      bun[2] /= gcd(bun[2], bun[3]);
      bun[3] /= gcd(bun[2], bun[3]);
      if (now.lbou != -1) {
        si[now.lbou] = bun[0];
        bo[now.lbou] = bun[1];
        que.push(now.lbou);
      } else {
        res = lcm(res, bun[1]);
      }
      if (now.rbou != -1) {
        si[now.rbou] = bun[2];
        bo[now.rbou] = bun[3];
        que.push(now.rbou);
      } else {
        res = lcm(res, bun[3]);
      }
    }
    cout << res << endl;
  }
  return 0;
}