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

#include <cmath>
#include <cstdio>
#include <queue>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)

struct stick {
  int red;
  int blue;
  int rnext;
  int bnext;
  int rm;
  int bm;
  int bef;
};

int labs(int a, int b) {
  if (a > b)
    return a - b;
  else
    return b - a;
}

stick sticks[102];
int GCM(int a, int b) {
  if (a == b || a % b == 0) {
    return b;
  }
  return GCM(min(a, b), labs(a, b));
}

signed main(void) {
  while (1) {
    int N;
    scanf("%lld", &N);
    if (N == 0)
      break;
    queue<int> que;
    rep(i, N) { // initialize
      sticks[i].bnext = -1;
      sticks[i].rnext = -1;
      sticks[i].bef = -1;
      sticks[i].bm = 1;
      sticks[i].rm = 1;
    }
    rep(i, N) {
      int p, q, r, b;
      scanf("%lld %lld %lld %lld", &p, &q, &r, &b);
      r--;
      b--;
      sticks[i].red = p / GCM(p, q);
      sticks[i].blue = q / GCM(p, q);
      if (r > -1) {
        sticks[i].rnext = r;
        sticks[r].bef = i;
      }
      if (b > -1) {
        sticks[i].bnext = b;
        sticks[b].bef = i;
      }
      if (r == -1 && b == -1) {
        que.push(i);
      }
    }
    // rep(i,N){printf("%lld %lld %lld
    // %lld\\n",i,sticks[i].bef,sticks[i].rnext,sticks[i].bnext);}
    int ans = 0;
    while (que.size()) {
      int n = que.front();
      que.pop();
      int be = sticks[n].bef;
      int A, B;
      A = sticks[n].bm * sticks[n].red / GCM(sticks[n].bm, sticks[n].red);
      B = sticks[n].rm * sticks[n].blue / GCM(sticks[n].rm, sticks[n].blue);
      int alpha = (A / sticks[n].red) * (B / sticks[n].blue) /
                  GCM(A / sticks[n].red, B / sticks[n].blue);
      if (be == -1) {
        ans = alpha * (sticks[n].blue + sticks[n].red);
        // printf("[[%lld]] %lld %lld %lld %lld
        // %lld/",n,A,B,sticks[n].blue,sticks[n].red,alpha);
        // break;
      } else {
        if (sticks[be].rnext == n) {
          sticks[be].rnext = -1;
          sticks[be].rm = alpha * (sticks[n].blue + sticks[n].red);
        } else {
          sticks[be].bnext = -1;
          sticks[be].bm = alpha * (sticks[n].blue + sticks[n].red);
        }
      }
      // printf("[%lld] %lld %lld %lld %lld
      // %lld/",n,A,B,sticks[n].blue,sticks[n].red,alpha);

      if (be > -1 && sticks[be].bnext == -1 && sticks[be].rnext == -1) {
        que.push(be);
        // printf("(%lld)",be);
        // break;
      }
    }
    printf("%lld\\n", ans);
  }
}

Error: 0

Fixed submission:

#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int INF = 1001001001;

const int mod = 100000;

#define MAX_N 100
int N;
pair<int, int> pq[MAX_N];
int rod[MAX_N][2];
vector<int> edge[MAX_N];
int redge[MAX_N][2];
priority_queue<pair<int, int>> dist; // second ??????top??????????????¢???first
bool iftop[MAX_N];                   // top??????false

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    memset(redge, -1, sizeof(redge));
    memset(iftop, false, sizeof(iftop));
    rep(i, N) {
      int p, q, r, b;
      scanf("%d %d %d %d", &p, &q, &r, &b);
      pq[i] = make_pair(p, q);
      if (r > 0) {
        iftop[r - 1] = true;
        edge[i].push_back(r - 1);
        redge[r - 1][0] = i;
      }
      if (b > 0) {
        iftop[b - 1] = true;
        edge[i].push_back(b - 1);
        redge[b - 1][1] = i;
      }
    }
    int top = find(iftop, iftop + N, false) - iftop;
    queue<int> q1;
    q1.push(top);
    int dister = 0;
    while (q1.size()) {
      queue<int> q2;
      while (q1.size()) {
        int a = q1.front();
        q1.pop();
        dist.push(make_pair(dister, a));
        rep(i, edge[a].size()) {
          q2.push(edge[a][i]);
          if (i > 0)
            break;
        }
      }
      q1 = q2;
      dister++;
    }

    int ans = 0;
    while (dist.size()) {
      pair<int, int> a = dist.top();
      dist.pop();
      int n = a.second;
      int gc = gcd(pq[n].first, pq[n].second);
      int p = pq[n].first / gc;
      int q = pq[n].second / gc;
      int sans = 0;
      if (rod[n][0] == 0 && rod[n][1] == 0) {
        sans = p + q;
      } else {
        if (rod[n][0] == 0)
          rod[n][0] = 1;
        if (rod[n][1] == 0)
          rod[n][1] = 1;
        int power = (rod[n][0] * p) * (rod[n][1] * q) /
                    gcd((rod[n][0] * p), (rod[n][1] * q));
        sans = power / p + power / q;
      }
      rep(i, 2) if (redge[n][i] > -1) { rod[redge[n][i]][i] += sans; }
      if (!dist.size()) {
        ans = sans;
      }
    }
    printf("%d\\n", ans);
  }
}