Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

#define MAX 101
using namespace std;

typedef int Integer;

Integer GCD(Integer a, Integer b) { return b == 0 ? a : GCD(b, a % b); }

struct Mobile {
  Mobile *parent, *pr, *pb;
  Integer p, q, gcdpq, dp, dq;
  Mobile() {
    parent = NULL;
    pr = NULL;
    pb = NULL;
  }
  bool realizable(Integer W) {
    if (W <= 1)
      return false;
    if ((W * gcdpq) % (p + q) != 0)
      return false;
    Integer k = (W * gcdpq) / (p + q);
    if (k <= 0)
      return false;
    bool br = true, bb = true;
    if (dq < dp) {
      if (pr) {
        br = pr->realizable(k * dq);
      }
      if (!br)
        return false;
      if (pb) {
        bb = pb->realizable(k * dp);
      }
      if (!bb)
        return false;
    } else {
      if (pb) {
        bb = pb->realizable(k * dp);
      }
      if (!bb)
        return false;
      if (pr) {
        br = pr->realizable(k * dq);
      }
      if (!br)
        return false;
    }
    return br && bb;
  }
};

int main() {
  while (true) {
    int n;
    Integer ans = -1;
    cin >> n;
    if (n == 0)
      break;
    Mobile *ms[MAX];
    for (int i = 0; i < n; ++i) {
      ms[i + 1] = new Mobile();
    }
    for (int i = 0; i < n; ++i) {
      Integer p, q, r, b;
      cin >> p >> q >> r >> b;
      ms[i + 1]->p = p;
      ms[i + 1]->q = q;
      ms[i + 1]->gcdpq = GCD(p, q);
      ms[i + 1]->dp = p / ms[i + 1]->gcdpq;
      ms[i + 1]->dq = q / ms[i + 1]->gcdpq;
      if (r > 0) {
        ms[r]->parent = ms[i + 1];
        ms[i + 1]->pr = ms[r];
      } else {
        ms[i + 1]->pr = NULL;
      }
      if (b > 0) {
        ms[b]->parent = ms[i + 1];
        ms[i + 1]->pb = ms[b];
      } else {
        ms[i + 1]->pb = NULL;
      }
    }
    for (int i = 1; i <= n; ++i) {
      if (ms[i]->parent == NULL) {
        for (Integer k = 1;; ++k) {
          Integer P = ms[i]->p;
          Integer Q = ms[i]->q;
          Integer gcdPQ = ms[i]->gcdpq;
          Integer W = (k * (P + Q)) / gcdPQ;
          if ((k * (P + Q)) % gcdPQ == 0) {
            if (ms[i]->realizable(W)) {
              ans = W;
              break;
            }
          }
        }
        break;
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define MAX 101
using namespace std;

typedef long long int Integer;

Integer GCD(Integer a, Integer b) { return b == 0 ? a : GCD(b, a % b); }

struct Mobile {
  Mobile *parent, *pr, *pb;
  Integer p, q, gcdpq, dp, dq;
  Mobile() {
    parent = NULL;
    pr = NULL;
    pb = NULL;
  }
  bool realizable(Integer W) {
    if (W <= 1)
      return false;
    if ((W * gcdpq) % (p + q) != 0)
      return false;
    Integer k = (W * gcdpq) / (p + q);
    if (k <= 0)
      return false;
    bool br = true, bb = true;
    if (dq < dp) {
      if (pr) {
        br = pr->realizable(k * dq);
      }
      if (!br)
        return false;
      if (pb) {
        bb = pb->realizable(k * dp);
      }
      if (!bb)
        return false;
    } else {
      if (pb) {
        bb = pb->realizable(k * dp);
      }
      if (!bb)
        return false;
      if (pr) {
        br = pr->realizable(k * dq);
      }
      if (!br)
        return false;
    }
    return br && bb;
  }
};

int main() {
  while (true) {
    int n;
    Integer ans = -1;
    cin >> n;
    if (n == 0)
      break;
    Mobile *ms[MAX];
    for (int i = 0; i < n; ++i) {
      ms[i + 1] = new Mobile();
    }
    for (int i = 0; i < n; ++i) {
      Integer p, q, r, b;
      cin >> p >> q >> r >> b;
      ms[i + 1]->p = p;
      ms[i + 1]->q = q;
      ms[i + 1]->gcdpq = GCD(p, q);
      ms[i + 1]->dp = p / ms[i + 1]->gcdpq;
      ms[i + 1]->dq = q / ms[i + 1]->gcdpq;
      if (r > 0) {
        ms[r]->parent = ms[i + 1];
        ms[i + 1]->pr = ms[r];
      } else {
        ms[i + 1]->pr = NULL;
      }
      if (b > 0) {
        ms[b]->parent = ms[i + 1];
        ms[i + 1]->pb = ms[b];
      } else {
        ms[i + 1]->pb = NULL;
      }
    }
    for (int i = 1; i <= n; ++i) {
      if (ms[i]->parent == NULL) {
        for (Integer k = 0;; ++k) {
          Integer P = ms[i]->p;
          Integer Q = ms[i]->q;
          Integer gcdPQ = ms[i]->gcdpq;
          Integer W = (k * (P + Q)) / gcdPQ;
          if ((k * (P + Q)) % gcdPQ == 0) {
            if (ms[i]->realizable(W)) {
              ans = W;
              break;
            }
          }
        }
        break;
      }
    }

    cout << ans << endl;
  }
  return 0;
}