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

#include <iostream>
#include <map>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void visit(const Graph &g, int v, vector<vector<int>> &scc, stack<int> &S,
           vector<bool> &inS, vector<int> &low, vector<int> &num, int &time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;
  FOR(e, g[v]) {
    int w = e->dst;
    if (num[w] == 0) {
      visit(g, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top();
      S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w)
        break;
    }
  }
}

void stronglyConnectedComponents(const Graph &g, vector<vector<int>> &scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time);
}

int main() {
  int n;
  while (cin >> n && n) {
    Graph G(n);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 2; ++j) {
        int b, f;
        cin >> b >> f;
        G[i].push_back(Edge(i, b, f));
      }
    }
    VVI scc;
    int res = 1;
    stronglyConnectedComponents(G, scc);
    for (auto it = scc.begin(); it != scc.end(); ++it) {
      VI V = *it;
      int m = V.size();
      int maxf = 0;
      for (int i = 0; i < m; ++i) {
        for (int j = 0; j < 2; ++j) {
          maxf = max(maxf, G[V[i]][j].weight);
        }
      }
      int cnt = 0;
      for (int i = 0; i < m; ++i) {
        for (int j = 0; j < 2; ++j) {
          if (maxf == G[V[i]][j].weight) {
            cnt++;
          }
        }
      }
      res = (res * (cnt / 2)) % 10007;
    }
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <stack>
#include <vector>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> PII;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void visit(const Graph &g, int v, vector<vector<int>> &scc, stack<int> &S,
           vector<bool> &inS, vector<int> &low, vector<int> &num, int &time) {
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v] = true;
  FOR(e, g[v]) {
    int w = e->dst;
    if (num[w] == 0) {
      visit(g, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top();
      S.pop();
      inS[w] = false;
      scc.back().push_back(w);
      if (v == w)
        break;
    }
  }
}
void stronglyConnectedComponents(const Graph &g, vector<vector<int>> &scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  REP(u, n) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time);
}

int main() {
  int n;
  while (cin >> n && n) {
    Graph G(n);
    map<PII, int> F;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 2; ++j) {
        int b, f;
        cin >> b >> f;
        G[i].push_back(Edge(i, b, f));
        F[PII(i, b)] = f;
      }
    }
    VVI scc;
    int res = 1;
    stronglyConnectedComponents(G, scc);
    for (auto it = scc.begin(); it != scc.end(); ++it) {
      VI V = *it;
      int n = V.size();
      int maxf = 0;
      for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
          maxf = max(maxf, F[PII(V[i], V[j])]);
        }
      }
      int cnt = 0;
      for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
          if (maxf == F[PII(V[i], V[j])]) {
            cnt++;
          }
        }
      }
      res = (res * cnt) % 10007;
    }
    cout << res << endl;
  }
  return 0;
}