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

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 30;
const double EPS = 1E-9;

map<string, bool> mp;
int n;

vector<string> make_list(string s) {
  vector<string> ls;
  int sz = s.size();
  int i = 0, j = 0;
  while (i < sz) {
    j = i;
    while (j < sz && isalpha(s[j]))
      j++;
    ls.push_back(s.substr(i, j - i));
    i = ++j;
  }
  return ls;
}

int main() {
  while (cin >> n && n) {
    vector<string> vs[100];
    string s;
    getline(cin, s);
    for (int i = 0; i < n; i++) {
      getline(cin, s);
      vs[i] = make_list(s);
      mp[vs[i][0]] = true;
    }
    bool update = true;
    while (update) {
      update = false;
      for (int j = 1; j < (int)vs[0].size(); j++) {
        if (mp[vs[0][j]]) {
          update = true;
          for (int k = 0; k < n; k++) {
            if (vs[0][j] == vs[k][0]) {
              for (int l = 1; l < (int)vs[k].size(); l++) {
                bool f = true;
                for (int m = 1; m < (int)vs[0].size(); m++) {
                  if (vs[0][m] == vs[k][l]) {
                    f = false;
                    break;
                  }
                }
                if (f)
                  vs[0].push_back(vs[k][l]);
              }
              break;
            }
          }
          vs[0].erase(vs[0].begin() + j);
          break;
        }
      }
    }
    int mem = vs[0].size() - 1;
    mp.clear();
    for (int i = 0; i < n; i++) {
      vs[i].clear();
    }
    cout << mem << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 30;
const double EPS = 1E-9;

map<string, int> mi;
map<string, bool> mp;
int n;

vector<string> make_list(string s) {
  vector<string> ls;
  int sz = s.size();
  int i = 0, j = 0;
  while (i < sz) {
    j = i;
    while (j < sz && isalpha(s[j]))
      j++;
    ls.push_back(s.substr(i, j - i));
    i = ++j;
  }
  return ls;
}

int main() {
  while (cin >> n && n) {
    vector<string> vs[100];
    string s;
    getline(cin, s);
    for (int i = 0; i < n; i++) {
      getline(cin, s);
      vs[i] = make_list(s);
      mp[vs[i][0]] = true;
    }
    bool update = true;
    while (update) {
      update = false;
      for (int i = 0; i < n; i++) {
        for (int j = 1; j < (int)vs[i].size(); j++) {
          if (mp[vs[i][j]]) {
            update = true;
            for (int k = 0; k < n; k++) {
              if (vs[i][j] == vs[k][0]) {
                for (int l = 1; l < (int)vs[k].size(); l++) {
                  vs[i].push_back(vs[k][l]);
                }
                break;
              }
            }
            vs[i].erase(vs[i].begin() + j);
            break;
          }
        }
      }
    }
    int mem = -1;
    mp.clear();
    for (int i = 0; i < n; i++) {
      for (int j = 1; j < (int)vs[i].size(); j++) {
        mi[vs[i][j]]++;
      }
      mem = max(mem, (int)mi.size());
      mi.clear();
      vs[i].clear();
    }
    cout << mem << endl;
  }
  return 0;
}