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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
class SCC { // cmp(0 - kk-1) is scc's return. Same number is group. The number
            // is no relatoin.
public:
  int n;
  vvi G, rG;
  vector<bool> used;
  vi vs, cmp;
  vvp cost;
  void init(int size) {
    n = size;
    G = rG = vvi(n);
    used = vector<bool>(n);
    cmp = vi(n);
    cost = vvp(n);
    vs = vi(0);
  }
  void add_edge(int s, int t, int c) {
    cost[s].pb(pii(t, c));
    G[s].pb(t);
    rG[t].pb(s);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    rep(i, rG[v].size()) if (!used[rG[v][i]]) rdfs(rG[v][i], k);
  }
  void dfs(int v) {
    used[v] = true;
    rep(i, G[v].size()) if (!used[G[v][i]]) dfs(G[v][i]);
    vs.pb(v);
  }
  int scc() {
    rep(i, n) used[i] = false;
    rep(v, n) if (!used[v]) dfs(v);
    rep(i, n) used[i] = false;
    int kk = 0;
    for (int i = vs.size() - 1; i >= 0; i--)
      if (!used[vs[i]])
        rdfs(vs[i], kk++);
    return kk;
  }
  void solve() {
    int N = scc();
    vi ma(N), co(N);
    rep(i, n) rep(j, cost[i].size()) {
      int t = cost[i][j].first;
      if (cmp[t] == cmp[i]) {
        ma[cmp[t]] = max(ma[cmp[t]], cost[i][j].second);
      }
    }
    rep(i, n) rep(j, cost[i].size()) {
      int t = cost[i][j].first;
      if (cmp[t] == cmp[i] && ma[cmp[t]] == cost[i][j].second) {
        co[cmp[t]]++;
      }
    }
    int out = 1;
    rep(i, N)(out *= co[i] / 2) %= 10007;
    cout << out << endl;
  }
};
int main() {
  int n;
  SCC scc;
  while (cin >> n, n) {
    scc.init(n);
    rep(i, n) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      scc.add_edge(i, a, b);
      scc.add_edge(i, c, d);
    }
    scc.solve();
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
class SCC { // cmp(0 - kk-1) is scc's return. Same number is group. The number
            // is no relatoin.
public:
  int n;
  vvi G, rG;
  vector<bool> used;
  vi vs, cmp;
  vvi cost;
  SCC(int size) {
    n = size;
    G = rG = vvi(n);
    used = vector<bool>(n);
    cmp = vi(n);
    cost = vvi(n, vi(n));
    vs = vi(0);
  }
  void add_edge(int s, int t, int c) {
    cost[s][t] = c;
    G[s].pb(t);
    rG[t].pb(s);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    rep(i, rG[v].size()) if (!used[rG[v][i]]) rdfs(rG[v][i], k);
  }
  void dfs(int v) {
    used[v] = true;
    rep(i, G[v].size()) if (!used[G[v][i]]) dfs(G[v][i]);
    vs.pb(v);
  }
  int scc() {
    rep(i, n) used[i] = false;
    rep(v, n) if (!used[v]) dfs(v);
    rep(i, n) used[i] = false;
    int kk = 0;
    for (int i = vs.size() - 1; i >= 0; i--)
      if (!used[vs[i]])
        rdfs(vs[i], kk++);
    return kk;
  }
  void solve() {
    int N = scc();
    vi ma(N), co(N);
    rep(i, n) rep(j, G[i].size()) {
      int t = G[i][j];
      if (cmp[t] == cmp[i]) {
        ma[cmp[t]] = max(ma[cmp[t]], cost[i][t]);
      }
    }
    rep(i, n) rep(j, G[i].size()) {
      int t = G[i][j];
      if (cmp[t] == cmp[i] && ma[cmp[t]] == cost[i][t]) {
        co[cmp[t]]++;
      }
    }
    int out = 1;
    rep(i, N)(out *= co[i] / 2) %= 10000;
    cout << out << endl;
  }
};
int main() {
  int n;
  while (cin >> n, n) {
    SCC scc(n);
    rep(i, n) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      scc.add_edge(i, a, b);
      scc.add_edge(i, c, d);
    }
    scc.solve();
  }
}