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

#include <iostream>
#include <vector>

long long int gcd(const long long int a, const long long int b) {
  switch (a % b) {
  case 0:
    return b;
  default:
    return gcd(b, a % b);
  }
}
long long int lcd(const long long int a, const long long int b) {
  return a * b / gcd(a, b);
}
class Mobile {
  long long int _left_length{0}, _right_length{0};
  Mobile *_left_mobile{nullptr}, *_right_mobile{nullptr};
  long long int _whole_weight{0};
  bool is_balanced() { return _whole_weight != 0; }
  void balance() {
    if (is_balanced())
      return;
    long long int l;
    if (_left_mobile == nullptr && _right_mobile == nullptr) {
      l = lcd(_left_length, _right_length);
    } else if (_left_mobile == nullptr) {
      _right_mobile->balance();
      l = lcd(_left_length, _right_length * _right_mobile->_whole_weight);
    } else if (_right_mobile == nullptr) {
      _left_mobile->balance();
      l = lcd(_right_length, _left_length * _left_mobile->_whole_weight);
    } else {
      _left_mobile->balance();
      _right_mobile->balance();
      l = lcd(_right_mobile->_whole_weight * _right_length,
              _left_mobile->_whole_weight * _left_length);
    }
    _whole_weight = l / _left_length + l / _right_length;
  }

public:
  Mobile() {}
  void set_infomation(long long int left, long long int right,
                      Mobile *left_mobile, Mobile *right_mobile) {
    _left_length = left / gcd(left, right);
    _right_length = right / gcd(left, right);
    _left_mobile = left_mobile;
    _right_mobile = right_mobile;
  }
  long long int whole_weight() {
    if (!is_balanced())
      balance();
    return _whole_weight;
  }
};
void solve() {
  int n;
  std::cin >> n;
  while (n != 0) {
    std::vector<Mobile> mobiles(n);
    {
      int p, q, r, b;
      for (auto &mobile : mobiles) {
        std::cin >> p >> q >> r >> b;
        mobile.set_infomation(p, q, (r == 0) ? nullptr : &mobiles[r - 1],
                              (b == 0) ? nullptr : &mobiles[b - 1]);
      }
      long long int max{0};
      for (auto &mobile : mobiles)
        if (max < mobile.whole_weight())
          max = mobile.whole_weight();
      std::cout << max << std::endl;
    }
    std::cin >> n;
  }
}
int main() { solve(); }


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

long long int gcd(const long long int a, const long long int b) {
  switch (a % b) {
  case 0:
    return b;
  default:
    return gcd(b, a % b);
  }
}
long long int lcd(const long long int a, const long long int b) {
  return a * b / gcd(a, b);
}
class Mobile {
  long long int _left_length{0}, _right_length{0};
  Mobile *_left_mobile{nullptr}, *_right_mobile{nullptr};
  long long int _whole_weight{0};
  bool is_balanced() { return _whole_weight != 0; }
  void balance() {
    long long int l;
    if (_left_mobile == nullptr && _right_mobile == nullptr) {
      l = lcd(_left_length, _right_length);
    } else if (_left_mobile == nullptr) {
      if (!_right_mobile->is_balanced())
        _right_mobile->balance();
      l = lcd(_left_length, _right_length * _right_mobile->_whole_weight);
    } else if (_right_mobile == nullptr) {
      if (!_left_mobile->is_balanced())
        _left_mobile->balance();
      l = lcd(_right_length, _left_length * _left_mobile->_whole_weight);
    } else
      l = lcd(_right_mobile->_whole_weight * _right_length,
              _left_mobile->_whole_weight * _left_length);
    _whole_weight = l / _left_length + l / _right_length;
  }

public:
  Mobile() {}
  void set_infomation(long long int left, long long int right,
                      Mobile *left_mobile, Mobile *right_mobile) {
    _left_length = left / gcd(left, right);
    _right_length = right / gcd(left, right);
    _left_mobile = left_mobile;
    _right_mobile = right_mobile;
  }
  long long int whole_weight() {
    if (!is_balanced())
      balance();
    return _whole_weight;
  }
};
void solve() {
  int n;
  std::cin >> n;
  while (n != 0) {
    std::vector<Mobile> mobiles(n);
    {
      int p, q, r, b;
      for (auto &mobile : mobiles) {
        std::cin >> p >> q >> r >> b;
        mobile.set_infomation(p, q, (r == 0) ? nullptr : &mobiles[r - 1],
                              (b == 0) ? nullptr : &mobiles[b - 1]);
      }
      long long int max{0};
      for (auto &mobile : mobiles)
        if (max < mobile.whole_weight())
          max = mobile.whole_weight();
      std::cout << max << std::endl;
    }
    std::cin >> n;
  }
}
int main() { solve(); }