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

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

typedef set<string> SS;
typedef map<string, bool> MSB;
typedef map<string, SS> MSS;
typedef pair<string, SS> PSS;

int n;
MSS L;
SS G;
SS T;
string start;

SS D;
SS V;
int solve(string group) {
  if (V.find(group) != V.end())
    return 0;
  V.insert(group);
  SS members = L[group];
  for (SS ::iterator it = members.begin(); it != members.end(); ++it) {
    string member = *it;
    if (G.find(member) == G.end()) {
      D.insert(member);
    } else {
      solve(member);
    }
  }
  return D.size();
}

int main(void) {
  string line;
  while (cin >> n && n) {
    L.clear();
    G.clear();
    T.clear();
    bool first = true;
    for (int i = 0; i < n; i++) {
      cin >> line;
      replace(line.begin(), line.end(), ',', ' ');
      replace(line.begin(), line.end(), '.', ' ');
      replace(line.begin(), line.end(), ':', ' ');
      string group, member;
      istringstream is(line);
      is >> group;
      if (first)
        start = group;
      first = false;
      G.insert(group);
      while (is >> member) {
        L[group].insert(member);
      }
    }

    for (MSS ::iterator it_i = L.begin(); it_i != L.end(); ++it_i) {
      SS list = (*it_i).second;
      for (SS ::iterator it_j = list.begin(); it_j != list.end(); ++it_j) {
        string member = *it_j;
        if (G.find(member) != G.end())
          T.insert((*it_i).first);
      }
    }

    D.clear();
    V.clear();
    int answer = solve(start);
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef set<string> SS;
typedef map<string, bool> MSB;
typedef map<string, SS> MSS;
typedef pair<string, SS> PSS;

int n;
MSS L;
SS G;
SS T;
string start;

SS D;
int solve(string group) {
  SS members = L[group];
  for (SS ::iterator it = members.begin(); it != members.end(); ++it) {
    string member = *it;
    if (G.find(member) == G.end()) {
      D.insert(member);
    } else {
      solve(member);
    }
  }
  return D.size();
}

int main(void) {
  string line;
  while (cin >> n && n) {
    L.clear();
    G.clear();
    T.clear();
    bool first = true;
    for (int i = 0; i < n; i++) {
      cin >> line;
      replace(line.begin(), line.end(), ',', ' ');
      replace(line.begin(), line.end(), '.', ' ');
      replace(line.begin(), line.end(), ':', ' ');
      string group, member;
      istringstream is(line);
      is >> group;
      if (first)
        start = group;
      first = false;
      G.insert(group);
      while (is >> member) {
        L[group].insert(member);
      }
    }

    for (MSS ::iterator it_i = L.begin(); it_i != L.end(); ++it_i) {
      SS list = (*it_i).second;
      for (SS ::iterator it_j = list.begin(); it_j != list.end(); ++it_j) {
        string member = *it_j;
        if (G.find(member) != G.end())
          T.insert((*it_i).first);
      }
    }

    D.clear();
    int answer = solve(start);
    cout << answer << endl;
  }
  return 0;
}