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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ff first
#define ss second
using namespace std;
vector<string> a[101];
map<string, int> m;
map<string, bool> f;
string ss, s;
int dfs(int u) {
  int ans = 0;
  int ll = a[u].size();
  for (int i = 0; i < ll; i++) {
    string v = a[u][i];
    if (f[v] == 1)
      continue;
    f[v] = 1;
    if (m.find(v) != m.end())
      ans += dfs(m[v]);
    else
      ans += 1;
  }
  return ans;
}
int main() {
  int i, j, T, N, S, W, Q;
  while (scanf("%d", &N) && N) {

    m.clear();
    f.clear();
    for (i = 0; i < N; i++) {
      a[i].clear();
      cin >> ss;
      bool ff = 1, o = 0;
      int en, k = 0;
      while (ff) {
        if (!o)
          en = ss.find(':', k);
        else {
          en = ss.find(',', k);
          if (en == -1) {
            en = ss.size() - 1;
            ff = 0;
          }
        }
        s.assign(ss, k, en - k);
        //               cout<<"@"<<s;
        k = en + 1;
        if (!o) {
          o = 1;
          m[s] = i;
          f[s] = 0;
        } else {
          a[i].push_back(s);
          f[s] = 0;
        }
      }
      /*
      a[i].clear() ;
      string s;
      scanf("%s",ss);
      j=0;
      while (ss[j]!=':') j++;
      ss[j]='\\0';
      s=ss;
      m[s]=i;f[s]=0;
      bool ff=1;
      while (ff)
      {
      j++;
      T=j;
      while (ss[j]!=','&&ss[j]!='.') j++;
      if (ss[j]=='.') ff=0;
      ss[j]='\\0';
      s=(ss+T);
      f[s]=0;
      a[i].push_back(s);

      }*/
    }
    printf("%d\\n", dfs(0));
  }
}
// freopen("video.in","r",stdin);
// freopen("video.out","w",stdout);

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000007
#define MP make_pair
const int maxn = 1010;
using namespace std;
typedef pair<int, int> P;
typedef long long LL;
int n, cnt;
map<string, int> mp;
int g[maxn][maxn], size[maxn], group[maxn], gnum;
bool vis[maxn];
int stk[maxn], top;
void get() {
  char c = getchar();
  while (c == ' ' || c == '\\n')
    c = getchar();
  string tmp = "";
  do {
    tmp += c;
    c = getchar();
  } while (c != ':');
  // cout << tmp << ':';
  if (mp.find(tmp) == mp.end())
    mp[tmp] = cnt++;
  int u = mp[tmp];
  group[gnum++] = u;
  do {
    c = getchar();
    tmp = "";
    do {
      tmp += c;
      c = getchar();
    } while (c != ',' && c != '.');
    if (mp.find(tmp) == mp.end())
      mp[tmp] = cnt++;
    g[u][size[u]++] = mp[tmp];
    // cout << tmp << ',';
  } while (c != '.');
  // cout << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
#endif
  int i, j = 0, u;
  while (~scanf("%d", &n) && n) {
    memset(size, 0, sizeof(size));
    memset(g, 0, sizeof(g));
    memset(group, 0, sizeof(group));
    memset(vis, 0, sizeof(vis));
    gnum = 0;
    cnt = 0;
    for (i = 0; i < n; i++)
      get();
    mp.clear();
    vis[0] = 1;
    top = 0;
    stk[top++] = 0;
    while (top > 0) {
      u = stk[--top];
      for (i = 0; i < size[u]; i++)
        if (!vis[g[u][i]]) {
          vis[g[u][i]] = 1;
          stk[top++] = g[u][i];
        }
    }
    for (i = 0; i < gnum; i++)
      vis[group[i]] = 0;
    int ans = 0;
    // cout << cnt << endl;
    for (i = 0; i < cnt; i++)
      if (vis[i])
        ans++;
    if (j)
      printf("\\n");
    printf("%d", ans);
    j++;
  }
  return 0;
}