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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

struct Stick {
  Int p, q, r, b;
};

vector<Stick> sticks;
vector<Int> weight;

Int gcd(Int a, Int b) {
  if (a > b)
    return gcd(b, a);
  else if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}

int main() {
  int n;
  while (n = in()) {
    sticks.clear();
    weight.clear();
    int i;
    Stick buf;
    for (i = 0; i < n; i++) {
      cin >> buf.p >> buf.q >> buf.r >> buf.b;
      buf.r--;
      buf.b--;
      sticks.push_back(buf);
      weight.push_back(0);
    }
    // for(i=0;i<n;i++)cout<<i<<" "<<sticks[i].p<<" "<<sticks[i].q<<"
    // "<<sticks[i].r<<" "<<sticks[i].b<<endl;
    bool update = true;
    while (update) {
      update = false;
      for (i = 0; i < n; i++) {
        if (weight[i] > 0)
          continue;
        if (sticks[i].r == -1 && sticks[i].b == -1) {
          weight[i] =
              (sticks[i].p + sticks[i].q) / gcd(sticks[i].p, sticks[i].q);
          update = true;
        } else if (sticks[i].r == -1 && weight[sticks[i].b] > 0) {
          Int r, b;
          b = weight[sticks[i].b] * sticks[i].p;
          r = b * sticks[i].q / sticks[i].p;
          weight[i] = (b + r) / gcd(r, sticks[i].p);
          update = true;
        } else if (sticks[i].b == -1 && weight[sticks[i].r] > 0) {
          Int r, b;
          r = weight[sticks[i].r] * sticks[i].q;
          b = r * sticks[i].p / sticks[i].q;
          weight[i] = (b + r) / gcd(b, sticks[i].q);
          update = true;
        } else if (weight[sticks[i].r] > 0 && weight[sticks[i].b] > 0) {
          Int r, b;
          b = weight[sticks[i].b] * weight[sticks[i].r] * sticks[i].p;
          r = b * sticks[i].q / sticks[i].p;
          weight[i] = (b + r) / gcd(weight[sticks[i].b] * sticks[i].q,
                                    weight[sticks[i].r] * sticks[i].p);
          update = true;
        }
      }
    }
    // for(i=0;i<n;i++)cout<<i<<" "<<weight[i]<<endl;
    Int res = 0;
    for (i = 0; i < n; i++) {
      chmax(res, weight[i]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

struct Stick {
  int p, q, r, b;
};

vector<Stick> sticks;
vector<int> weight;

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

int main() {
  int n;
  while (n = in()) {
    sticks.clear();
    weight.clear();
    int i;
    Stick buf;
    for (i = 0; i < n; i++) {
      cin >> buf.p >> buf.q >> buf.r >> buf.b;
      buf.r--;
      buf.b--;
      sticks.push_back(buf);
      weight.push_back(0);
    }
    // for(i=0;i<n;i++)cout<<i<<" "<<sticks[i].p<<" "<<sticks[i].q<<"
    // "<<sticks[i].r<<" "<<sticks[i].b<<endl;
    bool update = true;
    while (update) {
      update = false;
      for (i = 0; i < n; i++) {
        if (weight[i] > 0)
          continue;
        if (sticks[i].r == -1 && sticks[i].b == -1) {
          weight[i] =
              (sticks[i].p + sticks[i].q) / gcd(sticks[i].p, sticks[i].q);
          update = true;
        } else if (sticks[i].r == -1 && weight[sticks[i].b] > 0) {
          int r, b;
          b = weight[sticks[i].b] * sticks[i].p;
          r = b * sticks[i].q / sticks[i].p;
          weight[i] = (b + r) / gcd(r, sticks[i].p);
          update = true;
        } else if (sticks[i].b == -1 && weight[sticks[i].r] > 0) {
          int r, b;
          r = weight[sticks[i].r] * sticks[i].q;
          b = r * sticks[i].p / sticks[i].q;
          weight[i] = (b + r) / gcd(b, sticks[i].q);
          update = true;
        } else if (weight[sticks[i].r] > 0 && weight[sticks[i].b] > 0) {
          int r, b;
          b = weight[sticks[i].b] * weight[sticks[i].r] * sticks[i].p;
          r = b * sticks[i].q / sticks[i].p;
          weight[i] = (b + r) / gcd(weight[sticks[i].b] * sticks[i].q,
                                    weight[sticks[i].r] * sticks[i].p);
          update = true;
        }
      }
    }
    // for(i=0;i<n;i++)cout<<i<<" "<<weight[i]<<endl;
    int res = 0;
    for (i = 0; i < n; i++) {
      chmax(res, weight[i]);
    }
    cout << res << endl;
  }
  return 0;
}