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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

template <class T> T gcd(T a, T b) {
  if (a > b)
    swap(a, b);
  if (a == 0)
    return b;
  return gcd(a, b % a);
}

int barL[100];
int barR[100];

int barRatioL[100];
int barRatioR[100];

int which(int n) {
  vector<bool> f(n + 1, false);
  REP(i, n + 1) f[barL[i]] = f[barR[i]] = true;
  REP(i, n + 1) if (!f[i]) return i;
  return -1;
}

long long weight(int i) {
  int l = barL[i - 1];
  int r = barR[i - 1];

  long long wl = l == 0 ? 1 : weight(l);
  long long wr = r == 0 ? 1 : weight(r);

  int rl = barRatioL[i - 1];
  int rr = barRatioR[i - 1];

  long long R = wr * rr;
  long long L = wl * rl;
  long long g = gcd(R, L);
  long long lcm = R * L / g;

  return (lcm / rl + lcm / rr);
}

int main() {
  int n;
  while (n = getInt()) {
    REP(i, n) {
      int a = getInt();
      int b = getInt();
      int g = gcd(a, b);

      barL[i] = getInt();
      barR[i] = getInt();

      barRatioL[i] = a / g;
      barRatioR[i] = b / g;
    }

    printf("%lld\\n", weight(which(n)));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

template <class T> T gcd(T a, T b) {
  if (a > b)
    swap(a, b);
  if (a == 0)
    return b;
  return gcd(a, b % a);
}

int main() {
  int n;
  while (scanf("%d", &n), n) {
    vector<pair<int, int>> bar(n + 1);
    vector<pair<int, int>> rat(n + 1);

    vector<int> w(n + 1);
    vector<bool> flag(n + 1, false);
    flag[0] = true;
    vector<bool> dep(n + 1, false);
    REP(i, n) {
      int p, q, r, b;
      scanf("%d%d%d%d", &p, &q, &r, &b);
      int g = gcd(p, q);
      p /= g;
      q /= g;
      bar[i + 1].f = r;
      bar[i + 1].s = b;
      rat[i + 1].f = p;
      rat[i + 1].s = q;
      if (r == 0 && b == 0) {
        flag[i + 1] = true;
        w[i + 1] = p + q;
      }
    }

    bool f = true;
    while (f) {
      f = false;
      for (int i = 1; i < n + 1; i++) {
        if (!flag[i] && flag[bar[i].f] && flag[bar[i].s]) {
          int idx1 = bar[i].f;
          int idx2 = bar[i].s;

          int p = rat[i].f, q = rat[i].s;

          f = true;
          dep[idx1] = dep[idx2] = true;
          flag[i] = true;

          if (idx1 == 0) {
            w[i] = w[idx2] * (p + q);
          } else if (idx2 == 0) {
            w[i] = w[idx1] * (p + q);
          } else {
            int a = w[idx1];
            int b = w[idx2];
            int g = gcd(a, b);
            w[i] = a * b / g * q + b * a / g * p;
          }
        }
      }
    }
    int ans = 0;
    for (int i = 1; i < n + 1; i++)
      if (!dep[i])
        ans += w[i];
    printf("%d\\n", ans);
  }
  return 0;
}