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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
struct edge {
  int from, to;
  edge() {}
  edge(int from, int to) : from(from), to(to) {}
};
signed main() {
  const int MAX = 333;
  vector<vector<int>> G(MAX, vector<int>(MAX, 0));
  auto D = G;
  map<string, int> R;
  int sz = 0;

  {
    string p, q;
    cin >> p >> q;
    R[p] = sz++;
    R[q] = sz++;
  }

  int n;
  cin >> n;
  vector<vector<edge>> sG(n);
  vector<map<int, set<int>>> M(MAX);

  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      string s, t;
      cin >> s >> t;
      if (!R.count(s))
        R[s] = sz++;
      if (!R.count(t))
        R[t] = sz++;
      sG[i].emplace_back(R[s], R[t]);
      M[R[s]][R[t]].insert(i);
    }
  }

  vector<int> used(n, 0);
  queue<edge> q;
  auto add_edge = [&](int from, int to) {
    G[from][to] = 1;
    q.emplace(from, to);
  };
  auto add = [&](int x) {
    for (edge e : sG[x])
      add_edge(e.from, e.to);
  };

  add_edge(0, 1);

  while (!q.empty()) {
    edge e = q.front();
    q.pop();
    if (D[e.from][e.to])
      continue;
    D[e.from][e.to] = 1;
    // cout<<e.from<<" "<<e.to<<endl;
    for (int i : M[e.from][e.to]) {
      if (used[i])
        continue;
      used[i] = 1;
      add(i);
    }
    for (int i = 0; i < sz; i++) {
      if (G[i][e.from] && !D[i][e.to])
        q.emplace(i, e.to);
      if (G[e.to][i] && !D[e.from][i])
        q.emplace(e.from, i);
    }
  }

  for (int k = 0; k < MAX; k++)
    for (int i = 0; i < MAX; i++)
      for (int j = 0; j < MAX; j++)
        G[i][j] |= G[i][k] && G[k][j];

  for (int i = 0; i < MAX; i++) {
    if (!G[i][i])
      continue;
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
struct edge {
  int from, to;
  edge() {}
  edge(int from, int to) : from(from), to(to) {}
};
signed main() {
  const int MAX = 333;
  vector<vector<bool>> G(MAX, vector<bool>(MAX, 0));
  auto D = G;
  map<string, int> R;
  int sz = 0;

  {
    string p, q;
    cin >> p >> q;
    R[p] = sz++;
    R[q] = sz++;
  }

  int n;
  cin >> n;
  vector<vector<edge>> sG(n);
  vector<map<int, set<int>>> M(MAX);

  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      string s, t;
      cin >> s >> t;
      if (!R.count(s))
        R[s] = sz++;
      if (!R.count(t))
        R[t] = sz++;
      sG[i].emplace_back(R[s], R[t]);
      M[R[s]][R[t]].insert(i);
    }
  }

  vector<int> used(n, 0);
  queue<edge> q;
  auto add_edge = [&](int from, int to) {
    G[from][to] = 1;
    q.emplace(from, to);
  };
  auto add = [&](int x) {
    for (edge e : sG[x])
      add_edge(e.from, e.to);
  };

  add_edge(0, 1);

  while (!q.empty()) {
    edge e = q.front();
    q.pop();
    if (D[e.from][e.to])
      continue;
    D[e.from][e.to] = 1;
    // cout<<e.from<<" "<<e.to<<endl;
    for (int i : M[e.from][e.to]) {
      if (used[i])
        continue;
      used[i] = 1;
      add(i);
    }
    for (int i = 0; i < sz; i++) {
      if (G[i][e.from])
        q.emplace(i, e.to);
      if (G[e.to][i])
        q.emplace(e.from, i);
    }
  }

  for (int k = 0; k < MAX; k++)
    for (int i = 0; i < MAX; i++)
      for (int j = 0; j < MAX; j++)
        G[i][j] = G[i][j] || (G[i][k] && G[k][j]);

  for (int i = 0; i < MAX; i++) {
    if (!G[i][i])
      continue;
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  return 0;
}