alexjercan/bugnet huggingface dataset:
Problem p00442 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>

#define MAX_N 5000

using namespace std;

int N;
int E;
vector<int> in[MAX_N + 1];
vector<int> out[MAX_N + 1];
queue<int> S;
queue<int> L;

bool multi_flag;

bool in_exist(int v) {
  bool flag = false;
  if (in[v].empty())
    return false;
  for (int i = 0; i < in[v].size(); i++) {
    if (in[v][i] != -1)
      return true;
  }
  return false;
}

void solve() {
  for (int i = 1; i <= N; i++) {
    if (in_exist(i) == false)
      S.push(i);
  }

  while (!S.empty()) {
    int u = S.front();
    S.pop();
    L.push(u);
    int entered = 0;
    for (int i = 0; i < out[u].size(); i++) {
      int to = out[u][i];
      for (int i = 0; i < in[to].size(); i++) {
        if (in[to][i] == u)
          in[to][i] = -1;
      }

      if (in_exist(to) == false) {
        S.push(to);
        entered++;
      }
    }
    if (entered >= 2)
      multi_flag = true;
  }

  return;
}

int main() {

  scanf("%d", &N);
  scanf("%d", &E);
  // for(int i = 0;i <= MAX_N;i++){
  //	for(int j = 0;j <= MAX_N;j++){
  //		edge[i][j] = false;
  //	}
  // }
  int _u, _v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &_u, &_v);
    in[_v].push_back(_u);
    out[_u].push_back(_v);
  }

  solve();

  while (!L.empty()) {
    int team = L.front();
    L.pop();
    printf("%d\\n", team);
  }
  if (multi_flag)
    puts("1");
  else
    puts("0");

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>

#define MAX_N 5000

using namespace std;

int N;
int E;
bool edge[MAX_N + 1][MAX_N + 1];
queue<int> S;
queue<int> L;

bool multi_flag;

bool in_exist(int v) {
  bool flag = false;
  for (int i = 1; i <= N; i++) {
    if (edge[i][v])
      flag = true;
  }
  return flag;
}

void solve() {
  for (int i = 1; i <= N; i++) {
    if (in_exist(i) == false)
      S.push(i);
  }

  while (!S.empty()) {
    int u = S.front();
    S.pop();
    L.push(u);
    int entered = 0;
    for (int i = 1; i <= N; i++) {
      if (edge[u][i]) {
        edge[u][i] = false;
        if (in_exist(i) == false) {
          S.push(i);
          entered++;
        }
      }
    }
    if (entered >= 2)
      multi_flag = true;
  }

  return;
}

int main() {

  scanf("%d", &N);
  scanf("%d", &E);
  // for(int i = 0;i <= MAX_N;i++){
  //	for(int j = 0;j <= MAX_N;j++){
  //		edge[i][j] = false;
  //	}
  // }
  int _u, _v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &_u, &_v);
    edge[_u][_v] = true;
  }

  solve();

  while (!L.empty()) {
    int team = L.front();
    L.pop();
    printf("%d\\n", team);
  }
  if (multi_flag)
    puts("1");
  else
    puts("0");

  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int V, E, A, B;

vector<vector<int>> G;

vector<int> T;

vector<bool> used;

void rec(int u) {
  used[u] = true;

  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];

    if (!used[v]) {
      rec(v);
    }
  }

  T.push_back(u);
}

int main() {
  scanf("%d", &V);
  scanf("%d", &E);

  G = vector<vector<int>>(V);

  for (int i = 0; i < E; i++) {
    scanf("%d", &A);
    scanf("%d", &B);

    G[A - 1].push_back(B - 1);
  }

  used = vector<bool>(V, false);

  for (int i = 0; i < V; i++) {
    if (!used[i]) {
      rec(i);
    }
  }

  reverse(T.begin(), T.end());

  for (int i = 0; i < T.size(); i++) {
    printf("%d\\n", T[i] + 1);
  }

  bool ok = false;

  for (int i = 1; i < V; i++) {
    bool c = false;

    for (int j = 0; j < G[T[i - 1]].size(); j++) {
      if (G[T[i - 1]][j] == T[i]) {
        c = true;
      }
    }

    if (c == false) {
      ok = true;
    }
  }

  printf("%d\\n", ok ? 1 : 0);

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int V, E, A, B;

vector<vector<int>> G;

vector<int> T;

vector<bool> used;

void rec(int u) {
  used[u] = true;

  for (int i = 0; i < G[i].size(); i++) {
    int v = G[u][i];

    if (used[u]) {
      rec(v);
    }
  }

  T.push_back(u);
}

int main() {
  scanf("%d", &V);
  scanf("%d", &E);

  G = vector<vector<int>>(V);

  for (int i = 0; i < V; i++) {
    scanf("%d", &A);
    scanf("%d", &B);

    G[A - 1].push_back(B - 1);
  }

  int count_ = 0;

  for (int i = 0; i < V; i++) {
    if (used[i]) {
      rec(i);
      count_++;
    }
  }

  reverse(T.begin(), T.end());

  for (int i = 0; i < T.size(); i++) {
    printf("%d\\n", T[i]);
  }

  printf("%d\\n", count_ ? 1 : 0);

  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

// #define DEBUG
#define REP(i, n) for (int i = 1; i <= n; i++)
int n, m;
vector<int> g[5002];
int d[5002];

void mk(int i) {
  REP(k, g[i].size()) {
    int j = g[i][k - 1];
    if (d[i] + 1 > d[j]) {
      d[j] = d[i] + 1;
      mk(j);
    }
  }
}

int main() {
  cin >> n >> m;
  int a, b;
  REP(i, m) {
    cin >> a >> b;
    g[a].push_back(b);
  }
  REP(i, n) g[0].push_back(i);
  mk(0);
  bool f = 0;
  REP(r, n) {
    int c = 0;
    REP(i, n) {
      if (d[i] == r) {
        c++;
        cout << i << endl;
      }
    }
    if (c > 1)
      f = 1;
  }
  cout << f << endl;
#ifdef DEBUG
  REP(i, n) {
    REP(j, n) cout << g[i][j] << " ";
    cout << endl;
  }
  REP(i, n) cout << d[i] << " ";
#endif
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #define DEBUG
#define REP(i, n) for (int i = 1; i <= n; i++)
int n, m;
bool g[5002][5002];
int d[5002];

void mk(int i) {
  REP(j, n) if (g[i][j] && d[i] + 1 > d[j]) {
    d[j] = d[i] + 1;
    mk(j);
    REP(k, n) if (g[j][k] && d[j] + 1 > d[k] + 1) {
      d[k] = d[j] + 1;
      mk(k);
    }
  }
}

int main() {
  cin >> n >> m;
  int a, b;
  REP(i, m) {
    cin >> a >> b;
    g[a][b] = 1;
  }
  REP(i, n) g[0][i] = 1;
  mk(0);
  bool f = 0;
  REP(r, n) {
    int c = 0;
    REP(i, n) {
      if (d[i] == r) {
        c++;
        cout << i << endl;
      }
    }
    if (c > 1)
      f = 1;
  }
  cout << f << endl;
#ifdef DEBUG
  REP(i, n) {
    REP(j, n) cout << g[i][j] << " ";
    cout << endl;
  }
  REP(i, n) cout << d[i] << " ";
#endif
  return 0;
}
Problem p00442 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define MAX_V 5010
typedef vector<int> Vec;

int N, M;
Vec G[MAX_V];

bool visit(int v, Vec &order, Vec &color) {
  color[v] = 1;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int to = G[v][i];
    if (color[to] == 2)
      continue;
    if (color[to] == 1)
      return false;
    if (!visit(to, order, color))
      return false;
  }
  order.push_back(v);
  color[v] = 2;
  return true;
}

bool topologicalSort(Vec &order) {
  Vec color(N, 0);
  for (int i = 0; i < N; i++) {
    if (color[i] == 0 && !visit(i, order, color)) {
      return false;
    }
  }
  reverse(order.begin(), order.end());
  return true;
}

int main() {
  int s, t;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> s >> t;
    s--;
    t--;
    G[s].push_back(t);
  }
  Vec order;
  if (topologicalSort(order)) {
    bool is_hamiltonian_path = 1;
    for (int i = 0; i < N - 1; i++) {
      if (G[order[i]].size() == 0) {
        is_hamiltonian_path = 0;
      }
    }
    for (int i = 0; i < N; i++) {
      cout << order[i] + 1 << endl;
    }
    cout << !is_hamiltonian_path << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX_V 100010
typedef vector<int> Vec;

int N, M;
Vec G[MAX_V];

bool visit(int v, Vec &order, Vec &color) {
  color[v] = 1;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int to = G[v][i];
    if (color[to] == 2)
      continue;
    if (color[to] == 1)
      return false;
    if (!visit(to, order, color))
      return false;
  }
  order.push_back(v);
  color[v] = 2;
  return true;
}

bool topologicalSort(Vec &order) {
  Vec color(N, 0);
  for (int i = 0; i < N; i++) {
    if (color[i] == 0 && !visit(i, order, color)) {
      return false;
    }
  }
  reverse(order.begin(), order.end());
  return true;
}

int main() {
  int s, t;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> s >> t;
    G[s].push_back(t);
  }
  Vec order;
  if (topologicalSort(order)) {
    bool is_hamiltonian_path = 1;
    for (int i = 0; i < N - 1; i++) {
      if (G[order[i]].size() == 0) {
        is_hamiltonian_path = 0;
      }
    }
    for (int i = 0; i < N; i++) {
      cout << order[i] << endl;
    }
    cout << !is_hamiltonian_path << endl;
  }
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
struct bou {
  int l, r, x, y;
};
int n[100];
bou m[101];
int mincost(int a) {
  if (n[a - 1] != -1)
    return n[a - 1];
  int b = 1, c = 1;
  if (m[a].x)
    b = mincost(m[a].x);
  if (m[a].y)
    c = mincost(m[a].y);
  b /= gcd(b, m[a].r);
  c /= gcd(c, m[a].l);
  n[a - 1] = lcm(b, c) * (m[a].l + m[a].r);
  return lcm(b, c) * (m[a].l + m[a].r);
}
signed main() {
  int a;
  while (cin >> a, a) {
    memset(n, -1, sizeof(n));
    bou g;
    for (int b = 1; b <= a; b++) {
      int c, d;
      scanf("%lld%lld%lld%lld", &c, &d, &g.x, &g.y);
      g.l = c / gcd(c, d);
      g.r = d / gcd(c, d);
      m[b] = g;
    }
    for (int i = 0; i < a; i++) {
      if (n[i] == -1)
        mincost(i + 1);
    }
    int MAX = 0;
    for (int i = 0; i < a; i++) {
      MAX = max(MAX, n[i]);
    }
    cout << MAX << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
struct bou {
  int l, r, x, y;
};
int n[105];
bou m[105];
int mincost(int a) {
  if (n[a - 1] != -1)
    return n[a - 1];
  int b = 1, c = 1;
  if (m[a].x)
    b = mincost(mincost(m[a].x) / gcd(mincost(m[a].x), m[a].r));
  if (m[a].y)
    c = mincost(mincost(m[a].y) / gcd(mincost(m[a].y), m[a].l));
  n[a - 1] = lcm(b, c) * (m[a].l + m[a].r);
  return lcm(b, c) * (m[a].l + m[a].r);
}
signed main() {
  int a;
  while (cin >> a, a) {
    memset(n, -1, sizeof(n));
    bou g;
    for (int b = 1; b <= a; b++) {
      int c, d;
      scanf("%lld%lld%lld%lld", &c, &d, &g.x, &g.y);
      g.l = c / gcd(c, d);
      g.r = d / gcd(c, d);
      m[b] = g;
    }
    for (int i = 0; i < a; i++) {
      if (n[i] == -1)
        mincost(i + 1);
    }
    int MAX = 0;
    for (int i = 0; i < a; i++) {
      MAX = max(MAX, n[i]);
    }
    cout << MAX << endl;
  }
}
Problem p00443 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

long long int l[100];
long long int r[100];

long long int ln[100];
long long int rn[100];

bool use[100];

long long int ko(long long int a, long long int b) {
  while (1) {
    long long int w = a % b;
    if (w == 0) {
      return b;
    }
    a = b;
    b = w;
  }
}

long long int ju(int b) {
  long long int ld, rd;
  if (l[b] >= 0) {
    ld = ju(l[b]);
  } else {
    ld = 1;
  }
  if (r[b] >= 0) {
    rd = ju(r[b]);
  } else {
    rd = 1;
  }
  long long int lk = ld * ln[b];
  long long int rk = rd * rn[b];
  long long int ww = ko(ld, rd);
  long long int w = ko(lk, rk);
  long long int sai = lk * rk / w;
  return sai / ln[b] + sai / rn[b];
}

int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      int lne, rne;
      long long int lw, rw;
      scanf("%lld %lld %d %d", &lw, &rw, &lne, &rne);
      l[i] = lne - 1;
      r[i] = rne - 1;
      long long int w = ko(lw, rw);
      ln[i] = lw / w;
      rn[i] = rw / w;
    }
    for (int i = 0; i < n; i++) {
      use[i] = true;
    }
    for (int i = 0; i < n; i++) {
      if (l[i] >= 0) {
        use[l[i]] = false;
      }
      if (r[i] >= 0) {
        use[r[i]] = false;
      }
    }
    int oya;
    for (int i = 0; i < n; i++) {
      if (use[i]) {
        oya = i;
        break;
      }
    }
    printf("%lld\\n", ju(oya));
  }
  return 0;
}

Error: -8

Fixed submission:

#include <stdio.h>

long long int l[100];
long long int r[100];

long long int ln[100];
long long int rn[100];

bool use[100];

long long int ko(long long int a, long long int b) {
  while (1) {
    long long int w = a % b;
    if (w == 0) {
      return b;
    }
    a = b;
    b = w;
  }
}

long long int ju(int b) {
  long long int ld, rd;
  if (l[b] >= 0) {
    ld = ju(l[b]);
  } else {
    ld = 1;
  }
  if (r[b] >= 0) {
    rd = ju(l[b]);
  } else {
    rd = 1;
  }
  long long int lk = ld * ln[b];
  long long int rk = rd * rn[b];
  long long int w = ko(lk, rk);
  long long int sai = lk * rk / w;
  return sai / ln[b] + sai / rn[b];
}

int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      int lw, rw;
      scanf("%d %d", &lw, &rw);
      l[i] = lw - 1;
      r[i] = rw - 1;
    }
    for (int i = 0; i < n; i++) {
      use[i] = true;
    }
    for (int i = 0; i < 100; i++) {
      if (l[i] >= 0) {
        use[l[i]] = false;
      }
      if (r[i] >= 0) {
        use[r[i]] = false;
      }
    }
    for (int i = 0; i < n; i++) {
      long long int lw, rw;
      scanf("%lld %lld", &lw, &rw);
      long long int w = ko(lw, rw);
      ln[i] = lw / w;
      rn[i] = rw / w;
    }
    int oya;
    for (int i = 0; i < n; i++) {
      if (use[i]) {
        oya = i;
        break;
      }
    }
    printf("%lld\\n", ju(oya));
  }
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0520
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
inline ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct mobile {
  ll lb, rb, ln, rn;
  mobile() {}
  mobile(ll lb, ll rb, ll ln, ll rn) : lb(lb), rb(rb), ln(ln), rn(rn) {}
  mobile &operator=(const mobile &o) {
    this->lb = o.lb;
    this->rb = o.rb;
    this->ln = o.ln;
    this->rn = o.rn;
    return *this;
  }
};

ll dfs_mobile(int n, vector<mobile> &Mobile) {
  ll lM = Mobile[n].ln == 0 ? 1 : dfs_mobile(Mobile[n].ln, Mobile);
  ll rM = Mobile[n].rn == 0 ? 1 : dfs_mobile(Mobile[n].rn, Mobile);
  ll LCM = lcm(lM * Mobile[n].lb, rM * Mobile[n].rb);

  return LCM / Mobile[n].lb + LCM / Mobile[n].rb;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<ll> rev(n + 1, -1);
    vector<mobile> Mobile(n + 1);

    /* initialize */
    for (int i = 1; i <= n; i++) {
      ll lb, rb, ln, rn;
      cin >> lb >> rb >> ln >> rn;
      ll GCD = gcd(lb, rb);
      lb /= GCD;
      rb /= GCD;
      Mobile[i] = mobile(lb, rb, ln, rn);
      if (ln != 0)
        rev[ln] = i;
      if (rn != 0)
        rev[rn] = i;
    }

    /*????????¢?´¢*/
    ll root = 1;
    while (rev[root] != -1)
      root = rev[root];

    cout << dfs_mobile(root, Mobile) << endl;
  }
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0520
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
inline ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct mobile {
  ll lb, rb, ln, rn;
  mobile() {}
  mobile(ll lb, ll rb, ll ln, ll rn) : lb(lb), rb(rb), ln(ln), rn(rn) {}
  mobile &operator=(const mobile &o) {
    this->lb = o.lb;
    this->rb = o.rb;
    this->ln = o.ln;
    this->rn = o.rn;
    return *this;
  }
};

ll dfs_mobile(int n, vector<mobile> &Mobile) {
  if (n == 0)
    return 1;

  ll lM = dfs_mobile(Mobile[n].ln, Mobile);
  ll rM = dfs_mobile(Mobile[n].rn, Mobile);

  ll LCM = lcm(lM * Mobile[n].lb, rM * Mobile[n].rb);
  return LCM / Mobile[n].lb + LCM / Mobile[n].rb;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<ll> rev(n + 1, -1);
    vector<mobile> Mobile(n + 1);

    /* initialize */
    for (int i = 1; i <= n; i++) {
      ll lb, rb, ln, rn;
      cin >> lb >> rb >> rn >> ln;
      ll GCD = gcd(lb, rb);
      lb /= GCD;
      rb /= GCD;
      Mobile[i] = mobile(lb, rb, ln, rn);
      if (ln != 0)
        rev[ln] = i;
      if (rn != 0)
        rev[rn] = i;
    }

    /*????????¢?´¢*/
    ll root = 1;
    while (rev[root] != -1)
      root = rev[root];

    cout << dfs_mobile(root, Mobile) << endl;
  }
}
Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define X real()
#define Y imag()
using namespace std;
typedef complex<int> cld;
typedef vector<cld> vcl;
typedef vector<int> vi;
typedef long long ll;
ll lcs(ll p, ll q) {
  ll s = min(p, q), b = max(p, q);
  if (b % s == 0)
    return s;
  else
    return lcs(b % s, s);
}
ll gcd(ll p, ll q) { return p * q / lcs(p, q); }
int main() {
  int n;
  while (cin >> n && n) {
    bool fin[101] = {1, 0};
    ll mobil[101][4] = {};
    ll grav[101] = {1, 0};
    ll p, q, r, b;
    FOR(i, 1, n + 1) {
      cin >> p >> q >> r >> b;
      ll d = lcs(p, q);
      mobil[i][0] = q / d;
      mobil[i][1] = p / d;
      mobil[i][2] = r;
      mobil[i][3] = b;
    }
    REP(j, 101) {
      FOR(i, 1, n + 1)
      if (fin[mobil[i][2]] && fin[mobil[i][3]] && !fin[i]) {
        ll full = (mobil[i][0] + mobil[i][1]);
        ll needA = grav[mobil[i][2]] / lcs(grav[mobil[i][2]], mobil[i][0]);
        ll needB = grav[mobil[i][3]] / lcs(grav[mobil[i][3]], mobil[i][1]);
        grav[i] = full * (gcd(needA, needB));
        fin[i] = true;
      }
    }
    ll maxim = 0;
    FOR(i, 1, n + 1)
    maxim = max(maxim, grav[i]);
    cout << maxim << endl;
  }
  // your code goes here
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define X real()
#define Y imag()
using namespace std;
typedef complex<int> cld;
typedef vector<cld> vcl;
typedef vector<int> vi;
typedef long long ll;
ll lcs(ll p, ll q) {
  if (max(p, q) % min(p, q) == 0)
    return min(p, q);
  else
    return lcs(max(p, q) - min(p, q), min(p, q));
}
ll gcd(ll p, ll q) { return p * q / lcs(p, q); }
int main() {
  int n;
  while (cin >> n && n) {
    bool fin[101] = {1, 0};
    ll mobil[101][4] = {};
    ll grav[101] = {1, 0};
    ll p, q, r, b;
    FOR(i, 1, n + 1) {
      cin >> p >> q >> r >> b;
      ll d = lcs(p, q);
      mobil[i][0] = p / d;
      mobil[i][1] = q / d;
      mobil[i][2] = r;
      mobil[i][3] = b;
    }
    int finishes = 0;
    REP(j, 101) {
      FOR(i, 1, n + 1)
      if (fin[mobil[i][2]] && fin[mobil[i][3]] && !fin[i]) {
        grav[i] = gcd(grav[mobil[i][2]], grav[mobil[i][3]]) *
                  (mobil[i][0] + mobil[i][1]);
        fin[i] = true;
        finishes++;
      }
    }
    ll sum = 0;
    FOR(i, 1, n + 1)
    sum = max(sum, grav[i]);
    cout << sum << endl;
  }
  // your code goes here
  return 0;
}
Problem p00443 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main() {
  int n;
  int par[100], left[100], right[100], p[100], q[100], r[100], b[100];
  bool use[100];

  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      par[i] = -1;
    for (int i = 0; i < n; i++) {
      cin >> p[i] >> q[i] >> r[i] >> b[i];
      int tmp = gcd(p[i], q[i]);
      p[i] /= tmp;
      q[i] /= tmp;
      if (r[i]) {
        left[i] = 0;
        par[r[i] - 1] = i;
      } else
        left[i] = 1;
      if (b[i]) {
        right[i] = 0;
        par[b[i] - 1] = i;
      } else
        right[i] = 1;
      use[i] = false;
    }

    int ans;
    for (int j = 0; j < n; j++) {
      for (int i = 0; i < n; i++) {
        if (!use[i] && left[i] && right[i]) {
          int x = 1;
          while ((p[i] * x) % right[i] || (q[i] * x) % left[i])
            x++;
          right[i] = p[i] * x;
          left[i] = q[i] * x;
          ans = right[i] + left[i];
          // cout << left[i] << " " << right[i] << endl;
          if (0 <= par[i]) {
            if (r[par[i]] == i + 1)
              left[par[i]] = ans;
            else
              right[par[i]] = ans;
          }
          use[i] = true;
          break;
        }
      }
    }
    for (int i = 0; i < n; i++)
      if (par[i] < 0)
        cout << right[i] + left[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

typedef long long ll;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  int n;
  ll par[100], left[100], right[100], p[100], q[100], r[100], b[100];
  bool use[100];

  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      par[i] = -1;
    for (int i = 0; i < n; i++) {
      cin >> p[i] >> q[i] >> r[i] >> b[i];
      ll tmp = gcd(p[i], q[i]);
      p[i] /= tmp;
      q[i] /= tmp;
      if (r[i]) {
        left[i] = 0;
        par[r[i] - 1] = i;
      } else
        left[i] = 1;
      if (b[i]) {
        right[i] = 0;
        par[b[i] - 1] = i;
      } else
        right[i] = 1;
      use[i] = false;
    }

    bool f = true;
    ll ans;
    while (f) {
      for (int i = 0; i < n; i++) {
        if (!use[i] && left[i] && right[i]) {
          ll tmp = lcm(right[i], left[i]);
          right[i] = tmp * p[i];
          left[i] = tmp * q[i];
          ans = right[i] + left[i];
          if (0 <= par[i]) {
            if (r[par[i]] == i + 1)
              left[par[i]] = ans;
            else
              right[par[i]] = ans;
          } else {
            cout << ans << endl;
            f = false;
            break;
          }
          use[i] = true;
        }
      }
    }
  }
}