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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

long long int gcd(long long int l, long long int r) {
  assert(l > 0 && r > 0);
  if (l > r)
    return gcd(r, l);
  else {
    const long long int num = r % l;
    if (num) {
      return gcd(l, num);
    } else {
      return l;
    }
  }
}
long long int lca(long long int l, long long int r) {
  return l / gcd(l, r) * r;
}

struct mobi {
  long long int a;
  long long int b;
  int lid;
  int rid;
};

long long int getans(const int id, const vector<mobi> &ms) {
  long long int lw = 0, rw = 0;
  if (ms[id].lid != -1) {
    lw = getans(ms[id].lid, ms);
  } else {
    lw = 1;
  }
  if (ms[id].rid != -1) {
    rw = getans(ms[id].rid, ms);
  } else {
    rw = 1;
  }
  long long int al = lw * ms[id].a;
  long long int ar = rw * ms[id].b;
  long long int alca = lca(al, ar);
  return alca / al * lw + alca / ar * rw;
}

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<mobi> ms;
    vector<int> pas(N);
    for (int i = 0; i < N; ++i) {
      long long int a, b;
      int lid, rid;
      cin >> a >> b >> lid >> rid;
      lid--;
      rid--;
      if (lid != -1)
        pas[lid] = true;
      if (rid != -1)
        pas[rid] = true;
      const long long int agcd = gcd(a, b);
      a /= agcd;
      b /= agcd;
      ms.push_back(mobi{a, b, lid, rid});
    }
    int nopa = find(pas.begin(), pas.end(), 0) - pas.begin();
    long long int ans = getans(nopa, ms);
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

long long int gcd(long long int l, long long int r) {
  assert(l > 0 && r > 0);
  if (l > r)
    return gcd(r, l);
  else {
    const long long int num = r % l;
    if (num) {
      return gcd(l, num);
    } else {
      return l;
    }
  }
}
long long int lca(long long int l, long long int r) {
  return l / gcd(l, r) * r;
}

struct mobi {
  long long int a;
  long long int b;
  int lid;
  int rid;
};

long long int getans(const int id, const vector<mobi> &ms) {
  long long int lw = 0, rw = 0;
  if (ms[id].lid != -1) {
    lw = getans(ms[id].lid, ms);
  } else {
    lw = 1;
  }
  if (ms[id].rid != -1) {
    rw = getans(ms[id].rid, ms);
  } else {
    rw = 1;
  }
  long long int al = lw * ms[id].b;
  long long int ar = rw * ms[id].a;
  long long int alca = lca(al, ar);
  return alca / al * lw + alca / ar * rw;
}

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<mobi> ms;
    vector<int> pas(N);
    for (int i = 0; i < N; ++i) {
      long long int a, b;
      int lid, rid;
      cin >> a >> b >> lid >> rid;
      lid--;
      rid--;
      if (lid != -1)
        pas[lid] = true;
      if (rid != -1)
        pas[rid] = true;
      const long long int agcd = gcd(a, b);
      a /= agcd;
      b /= agcd;
      ms.push_back(mobi{a, b, lid, rid});
    }
    int nopa = find(pas.begin(), pas.end(), 0) - pas.begin();
    long long int ans = getans(nopa, ms);
    cout << ans << endl;
  }
  return 0;
}