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

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

pair<string, vector<string>> parse(string s) {
  for (int i = 0; i < s.size(); i++)
    if (!isalpha(s[i]))
      s[i] = ' ';

  s.erase(s.end() - 1);

  stringstream ss(s);
  string n;
  ss >> n;

  string a;
  vector<string> res;
  while (ss >> a) {
    res.push_back(a);
  }

  return make_pair(n, res);
}

bool vis[100];
set<string> ans;
void dfs(int u, map<string, int> &dic, vector<vector<string>> &graph) {
  vis[u] = true;

  for (int i = 0; i < graph[u].size(); i++) {
    string n = graph[u][i];
    if (dic.count(n)) {
      int v = dic[n];
      if (vis[v])
        continue;
      dfs(v, dic, graph);
    } else
      ans.insert(n);
  }
}

int main() {
  int N;
  while (cin >> N, N) {
    map<string, int> dic;
    vector<vector<string>> graph(N);

    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;

      pair<string, vector<string>> p = parse(s);
      string n = p.first;
      dic[n] = i;

      for (int j = 0; j < p.second.size(); j++)
        graph[i].push_back(p.second[j]);
    }

    memset(vis, 0, sizeof(vis));
    ans.clear();
    dfs(0, dic, graph);

    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define qu queue<int>
#define vc vector<int>
#define mci map<string, int>
#define mm(o) memset(o, 0, sizeof(o))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("out.txt", "w", stdout)
#define pii pair<int, int>
#define rep(i, start, end) for (int i = start; i <= end; i++)
#define at                                                                     \\
  int tt;                                                                      \\
  scanf("%d", &tt);                                                            \\
  for (int t = 1; t <= tt; t++)
#define pi (double)(2 * acos(0.0))
#define LL long long
#define MAXKEYS (300)

using namespace std;

vector<string> v[110];
map<string, int> mp;
map<int, string> mp1;
set<string> st;

void func(int n);

int main() {
  //    op;
  while (1) {
    int n;
    scanf("%d", &n);
    //       cout<<n<<endl;
    if (n == 0)
      break;

    for (int i = 0; i <= n; i++)
      v[i].clear();

    mp.clear();
    mp1.clear();
    st.clear();

    int k = 0;
    for (int i = 1; i <= n; i++) {
      string s;
      cin >> s;
      //           cout<<s<<endl;
      int sz = s.size();
      string s1;
      for (int j = 0; j < sz; j++) {
        char ch = s[j];

        if (ch == ':') {
          //                cout<<s1<<" ";
          if (!mp[s1]) {
            mp[s1] = ++k;
            mp1[k] = s1;
          }
          s1.clear();
        }

        else if (ch == ',' || ch == '.') {
          //                 cout<<s1<<" ";
          v[k].push_back(s1);
          s1.clear();
        } else
          s1 += ch;
      }
    }

    func(1);
    int szz = st.size();
    cout << szz << endl;
  }
  return 0;
}

void func(int n) {

  int sz = v[n].size();

  for (int i = 0; i < sz; i++) {
    string s = v[n][i];
    if (!mp[s])
      st.insert(s);
    else
      func(mp[s]);
  }
}