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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, e) for (int i = int(s); i != int(e); i++)
#define FORIT(i, c)                                                            \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ISEQ(c) (c).begin(), (c).end()
typedef long long ll;

map<string, set<string>> M;
set<string> S;
map<string, bool> dp;
void searchMemb(string request) {
  if (dp[request])
    return;
  dp[request] = true;
  if (S.count(request) == 1)
    return;
  FORIT(itr, M[request]) {
    if (M.find(*itr) != M.end() && M[*itr].size() != 0) {
      searchMemb(*itr);
    } else {
      if (S.count(*itr) == 0) {
        S.insert(*itr);
      }
    }
  }
}

int main() {
  while (true) {
    M.clear();
    S.clear();
    dp.clear();
    ll n;
    cin >> n;
    if (n == 0)
      break;
    char request[16];
    rep(l, n) {
      char in[180];
      cin >> in;
      int in_size = strlen(in);
      // parse
      rep(i, in_size) {
        if (in[i] == ',' || in[i] == '.' || in[i] == ':') {
          in[i] = '\\0';
        }
      }
      char parent[20];
      strcpy(parent, in);
      if (l == 0)
        strcpy(request, parent);
      FOR(i, strlen(in) + 1, in_size) {
        char child[20];
        strcpy(child, in + i);
        M[parent].insert(child);
        i += strlen(in + i);
      }
    }

    //		FORIT(itr,M){
    //			cout << itr->first << endl;
    //			FORIT(veci,itr->second.children){
    //				cout << *veci << " ";
    //			}
    //			cout << endl;
    //		}
    searchMemb(request);
    cout << S.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, s, e) for (int i = int(s); i != int(e); i++)
#define FORIT(i, c)                                                            \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ISEQ(c) (c).begin(), (c).end()

map<string, vector<string>> M;
set<string> S;
int searchMemb(string request) {
  int cnt = 0;
  FORIT(itr, M[request]) {
    if (M.find(*itr) != M.end()) {
      cnt += searchMemb(*itr);
    } else {
      if (S.count(*itr) == 0) {
        S.insert(*itr);
        cnt++;
      }
    }
  }
  return cnt;
}

int main() {
  while (true) {
    M.clear();
    S.clear();
    int n;
    cin >> n;
    if (n == 0)
      break;
    char request[16];
    rep(l, n) {
      char in[180];
      cin >> in;
      int in_size = strlen(in);
      // parse
      rep(i, in_size) {
        if (in[i] == ',' || in[i] == '.' || in[i] == ':') {
          in[i] = '\\0';
        }
      }
      char parent[20];
      strcpy(parent, in);
      if (l == 0)
        strcpy(request, parent);
      FOR(i, strlen(in) + 1, in_size) {
        char child[20];
        strcpy(child, in + i);
        M[parent].push_back(child);
        i += strlen(in + i);
      }
    }

    //		FORIT(itr,M){
    //			cout << itr->first << endl;
    //			FORIT(veci,itr->second.children){
    //				cout << *veci << " ";
    //			}
    //			cout << endl;
    //		}
    cout << searchMemb(request) << endl;
  }
  return 0;
}