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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int p[101], q[101], r[101], b[101];
int dfs(int s) {
  if (s == 0)
    return 1;

  int red = dfs(r[s]), blue = dfs(b[s]);
  int t = lcm(red * p[s], blue * q[s]);
  return t / p[s] + t / q[s];
}
int main() {
  int n;
  while (~scanf("%d", &n) && n) {
    bool root[101];
    fill(root, root + 101, true);
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d%d%d", p + i, q + i, r + i, b + i);
      root[r[i]] = root[b[i]] = false;
    }

    int res = 0;
    for (int i = 1; i <= n; ++i)
      if (root[i])
        res = dfs(i);
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int p[101], q[101], r[101], b[101];
int dfs(int s) {
  if (s == 0)
    return 1;

  int red = dfs(r[s]), blue = dfs(b[s]);
  int t = lcm(red * q[s], blue * p[s]);
  return t / p[s] + t / q[s];
}
int main() {
  int n;
  // while (~scanf("%d", &n) && n)
  while (cin >> n, n) {
    bool root[101];
    fill(root, root + 101, true);
    for (int i = 1; i <= n; ++i) {
      // scanf("%d%d%d%d", p+i, q+i, r+i, b+i);
      cin >> p[i] >> q[i] >> r[i] >> b[i];
      root[r[i]] = root[b[i]] = false;
    }

    int res = 0;
    for (int i = 1; i <= n; ++i)
      if (root[i])
        res = dfs(i);
    printf("%d\\n", res);
  }
}