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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;
struct mobile {
  ll rc;
  ll lc;
  ll l;
  ll r;
  ll w;
};

ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }

int main() {
  while (true) {
    ll n;
    cin >> n;
    if (n == 0)
      break;
    ll ans = 0;
    vector<struct mobile> v(n);
    REP(i, n) {
      ll l, r, lc, rc;
      cin >> l >> r >> lc >> rc;
      v[i].l = l;
      v[i].r = r;
      v[i].lc = lc;
      v[i].rc = rc;
      v[i].w = 0;
    }
    bool flg = true;
    while (flg) {
      flg = false;
      REP(i, n) {
        if (v[i].w == 0 && v[i].rc == 0 && v[i].lc == 0) {
          ll r = v[i].r;
          ll l = v[i].l;
          v[i].r /= gcd(r, l);
          v[i].l /= gcd(r, l);
          v[i].w = v[i].r + v[i].l;
          flg = true;
        } else {
          ll lw, rw;
          if (v[i].w > 0)
            continue;
          if (v[i].lc == 0) {
            lw = 1;
          } else if (v[v[i].lc - 1].w > 0) {
            lw = v[v[i].lc - 1].w;
          } else
            continue;

          if (v[i].rc == 0) {
            rw = 1;
          } else if (v[v[i].rc - 1].w > 0) {
            rw = v[v[i].rc - 1].w;
          } else
            continue;
          ll r, l;
          r = v[i].r;
          l = v[i].l;
          v[i].r /= gcd(r, l);
          v[i].l /= gcd(r, l);
          ll w = 1000000000000000;

          FOR(j, 1, 1000000) {
            if ((lw * v[i].l * j) % v[i].r == 0) {
              if (((lw * v[i].l * j) / v[i].r) % rw == 0) {
                w = lw * j + (lw * v[i].l * j) / v[i].r;
                /*
                printf("l = %d r = %d\\n",v[i].l,v[i].r);
                printf("lw = %d rw = %d\\n",lw*j,(lw*v[i].l*j)/v[i].r);
                printf("w = %d j = %d\\n",w,j);
                */
                break;
              }
            }
            if (w < (1 << 31))
              break;
          }
          v[i].w = w;
          flg = true;
        }
        ans = max(ans, v[i].w);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;
struct mobile {
  ll rc;
  ll lc;
  ll l;
  ll r;
  ll w;
};

ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }

int main() {
  while (true) {
    ll n;
    cin >> n;
    if (n == 0)
      break;
    ll ans = 0;
    vector<struct mobile> v(n);
    REP(i, n) {
      ll l, r, lc, rc;
      cin >> l >> r >> lc >> rc;
      v[i].l = l;
      v[i].r = r;
      v[i].lc = lc;
      v[i].rc = rc;
      v[i].w = 0;
    }
    bool flg = true;
    while (flg) {
      flg = false;
      REP(i, n) {
        if (v[i].w == 0 && v[i].rc == 0 && v[i].lc == 0) {
          ll r = v[i].r;
          ll l = v[i].l;
          v[i].r /= gcd(r, l);
          v[i].l /= gcd(r, l);
          v[i].w = v[i].r + v[i].l;
          flg = true;
        } else {
          ll lw, rw;
          if (v[i].w > 0)
            continue;
          if (v[i].lc == 0) {
            lw = 1;
          } else if (v[v[i].lc - 1].w > 0) {
            lw = v[v[i].lc - 1].w;
          } else
            continue;

          if (v[i].rc == 0) {
            rw = 1;
          } else if (v[v[i].rc - 1].w > 0) {
            rw = v[v[i].rc - 1].w;
          } else
            continue;
          ll r, l;
          r = v[i].r;
          l = v[i].l;
          v[i].r /= gcd(r, l);
          v[i].l /= gcd(r, l);
          ll w = 1000000000000000;
          FOR(k, 1, 10000000) {
            FOR(j, 1, 100000000) {
              if (rw * k * v[i].r == lw * j * v[i].l) {
                w = min(w, rw * k + lw * j);
                break;
              }
            }
          }
          v[i].w = w;
          flg = true;
        }
        ans = max(ans, v[i].w);
      }
    }
    cout << ans << endl;
  }
  return 0;
}