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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

map<string, set<string>> memo;

set<string> foo(string t, map<string, vector<string>> &v) {
  if (memo.count(t))
    return memo[t];
  set<string> res;
  for (int i = 0; i < v[t].size(); i++) {
    string s = v[t][i];
    if (v.count(s)) {
      set<string> st = foo(s, v);
      for (set<string>::iterator it = st.begin(); it != st.end(); it++)
        res.insert(*it);
    } else
      res.insert(s);
  }
  return memo[t] = res;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    string t;
    map<string, vector<string>> v;
    for (int k = 0; k < n; k++) {
      string s;
      cin >> s;
      int i = 0;
      for (; s[i] != ':'; i++)
        ;
      string g = s.substr(0, i);
      if (k == 0)
        t = g;
      i++;
      for (;;) {
        int _i = i;
        for (; s[i] != ',' && s[i] != '.'; i++)
          ;
        string m = s.substr(_i, i - _i);
        v[g].push_back(m);
        if (s[i] == '.')
          break;
        i++;
      }
    }
    memo.clear();
    cout << foo(t, v).size() << endl;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

set<string> foo(string t, map<string, vector<string>> &v) {
  set<string> res;
  for (int i = 0; i < v[t].size(); i++) {
    string s = v[t][i];
    if (v.count(s)) {
      set<string> st = foo(s, v);
      for (set<string>::iterator it = st.begin(); it != st.end(); it++)
        res.insert(*it);
    } else
      res.insert(s);
  }
  return res;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    string t;
    map<string, vector<string>> v;
    for (int k = 0; k < n; k++) {
      string s;
      cin >> s;
      int i = 0;
      for (; s[i] != ':'; i++)
        ;
      string g = s.substr(0, i);
      if (k == 0)
        t = g;
      i++;
      for (;;) {
        int _i = i;
        for (; s[i] != ',' && s[i] != '.'; i++)
          ;
        string m = s.substr(_i, i - _i);
        v[g].push_back(m);
        if (s[i] == '.')
          break;
        i++;
      }
    }
    cout << foo(t, v).size() << endl;
  }
}