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

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

int N;
list<int> P;
list<long long int> ans;

int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  P.push_back(2);
  for (int i = 3; i < pow(2, 16); i += 2) {
    bool flag = true;
    for (auto j : P) {
      if (i % j == 0) {
        flag = false;
        break;
      }
      if (j * j > i)
        break;
    }
    if (flag)
      P.push_back(i);
  }
  while (N) {
    vector<long long int> l(N);
    vector<long long int> r(N);
    vector<int> lx(N);
    vector<int> rx(N);
    vector<long long int> lw(N);
    vector<long long int> rw(N);
    vector<bool> flag(N, true);
    stack<int> node;
    for (int i = 0; i < N; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      c--;
      d--;
      for (auto i : P) {
        while (a % i == 0 && b % i == 0) {
          a /= i;
          b /= i;
        }
        if (min(a, b) < i)
          break;
      }
      l[i] = a;
      r[i] = b;
      lx[i] = c;
      rx[i] = d;
      if (c == -1 && d == -1) {

        lw[i] = r[i];
        rw[i] = l[i];
      } else if (c == -1)
        lw[i] = 1;
      else if (d == -1)
        rw[i] = 1;
      if (c >= 0)
        flag[c] = false;
      if (d >= 0)
        flag[d] = false;
    }
    //	cout << "hijiki";
    for (int i = 0; i < N; i++) {
      if (flag[i]) {
        node.push(i);
      }
    }
    while (!node.empty()) {
      int current = node.top();
      bool con = false;
      if (lx[current] >= 0) {
        if (lw[lx[current]] && rw[lx[current]])
          lw[current] = lw[lx[current]] + rw[lx[current]];
        else {
          node.push(lx[current]);
          con = true;
        }
      }
      if (rx[current] >= 0) {
        if (lw[rx[current]] && rw[rx[current]])
          rw[current] = lw[rx[current]] + rw[rx[current]];
        else {
          node.push(rx[current]);
          continue;
        }
      }
      if (con)
        continue;
      if (l[current] * lw[current] != r[current] * rw[current]) {
        long long int a = r[current] * rw[current];
        long long int b = l[current] * lw[current];
        for (auto i : P) {
          while (a % i == 0 && b % i == 0) {
            a /= i;
            b /= i;
          }
          if (min(a, b) < i)
            break;
        }
        lw[current] *= a;
        rw[current] *= b;
      }
      //	cout << current << " " << lw[current] << " " << rw[current] <<
      //endl;
      node.pop();
    }
    for (int i = 0; i < N; i++) {
      if (flag[i]) {
        ans.push_back(lw[i] + rw[i]);
        // cout << lw[i] + rw[i] << endl;
        break;
      }
    }
    cin >> N;
  }
  for (auto i : ans)
    cout << i << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

int N;
list<int> P;

int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  P.push_back(2);
  for (int i = 3; i < pow(2, 32); i += 2) {
    bool flag = true;
    for (auto j : P) {
      if (i % j == 0) {
        flag = false;
        break;
      }
      if (j * j > i)
        break;
    }
    if (flag)
      P.push_back(i);
  }
  while (N) {
    vector<long long int> l(N);
    vector<long long int> r(N);
    vector<int> lx(N);
    vector<int> rx(N);
    vector<long long int> lw(N);
    vector<long long int> rw(N);
    vector<bool> flag(N, true);
    stack<int> node;
    for (int i = 0; i < N; i++) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      c--;
      d--;
      l[i] = a;
      r[i] = b;
      lx[i] = c;
      rx[i] = d;
      if (c == -1)
        lw[i] = 1;
      if (d == -1)
        rw[i] = 1;
      if (c >= 0)
        flag[c] = false;
      if (d >= 0)
        flag[d] = false;
    }
    //	cout << "hijiki";
    for (int i = 0; i < N; i++) {
      if (flag[i]) {
        node.push(i);
      }
    }
    while (!node.empty()) {
      int current = node.top();
      if (lx[current] >= 0) {
        if (lw[lx[current]] && rw[lx[current]])
          lw[current] = lw[lx[current]] + rw[lx[current]];
        else {
          node.push(lx[current]);
          continue;
        }
      }
      if (rx[current] >= 0) {
        if (lw[rx[current]] && rw[rx[current]])
          rw[current] = lw[rx[current]] + rw[rx[current]];
        else {
          node.push(rx[current]);
          continue;
        }
      }
      if (l[current] * lw[current] != r[current] * rw[current]) {
        long long int a = r[current] * rw[current];
        long long int b = l[current] * lw[current];
        for (auto i : P) {
          while (a % i == 0 && b % i == 0) {
            a /= i;
            b /= i;
          }
          if (min(a, b) < i)
            break;
        }
        lw[current] *= a;
        rw[current] *= b;
      }
      node.pop();
    }
    for (int i = 0; i < N; i++) {
      if (flag[i]) {
        cout << lw[i] + rw[i] << endl;
        break;
      }
    }
    cin >> N;
  }
  return 0;
}