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

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct Group {
  string name;
  set<string> member;
};

struct Group group[100];
bool use[100];
int n;

void open(int x) {
  bool f;
  set<string>::iterator ite;
  f = false;
  for (ite = group[x].member.begin(); ite != group[x].member.end(); ite++) {
    if (f)
      ite = group[x].member.begin();
    f = false;
    for (int i = 0; i < n; i++) {
      if (group[i].name == *ite) {
        // cout << "[" << *ite << "]" << endl;
        group[x].member.erase(ite);
        /*if(!use[i])*/ open(i);
        set<string>::iterator j;
        for (j = group[i].member.begin(); j != group[i].member.end(); j++) {
          group[x].member.insert(*j);
        }
        f = true;
        break;
      }
    }
  }
  use[x] = true;
}

int main() {
  int i;
  int c;
  string tmp, tmp2;

  while (1) {
    cin >> n;
    if (!n)
      break;

    for (i = 0; i < n; i++) {
      group[i].name.clear();
      group[i].member.clear();
    }

    for (i = 0; i < n; i++) {
      cin >> tmp;
      c = 0;
      while (tmp[c] != ':') {
        group[i].name.push_back(tmp[c]);
        c++;
      }
      while (tmp[c] != '.') {
        c++;
        while (tmp[c] != ',' && tmp[c] != '.') {
          tmp2.push_back(tmp[c]);
          c++;
        }
        group[i].member.insert(tmp2);
        tmp2.clear();
      }
    }

    for (i = 0; i < n; i++)
      use[i] = false;
    open(0);
    /*
    for(int i=0;i<n;i++){
      set<string>::iterator ite;
      for(ite = group[i].member.begin();ite!=group[i].member.end();ite++){
        cout<< *ite << " ";
      }
      cout << endl;
    }
    */
    cout << group[0].member.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

struct Group {
  string name;
  set<string> member;
  int num;
};

int main() {
  int i;
  int n;
  int c;
  int num;
  struct Group group[100];
  string tmp, tmp2;

  while (1) {
    cin >> n;
    if (!n)
      break;

    for (i = 0; i < n; i++) {
      cin >> tmp;
      c = 0;
      while (tmp[c] != ':') {
        group[i].name.push_back(tmp[c]);
        c++;
      }
      num = 0;
      while (tmp[c] != '.') {
        c++;
        while (tmp[c] != ',' && tmp[c] != '.') {
          tmp2.push_back(tmp[c]);
          c++;
        }
        group[i].member.insert(tmp2);
        tmp2.clear();
        num++;
      }
      group[i].num = num;
    }

    set<string>::iterator it1 = group[0].member.begin();
    while (it1 != group[0].member.end()) {
      for (i = 1; i < n; i++) {
        if (*it1 == group[i].name) {
          group[0].member.erase(it1);
          set<string>::iterator it2 = group[i].member.begin();
          while (it2 != group[i].member.end()) {
            group[0].member.insert(*it2);
            it2++;
          }
          it1 = group[0].member.begin();
          goto L1;
        }
      }
      it1++;
    L1:;
    }

    cout << group[0].member.size() << endl;

    for (i = 0; i < n; i++) {
      group[i].name.clear();
      group[i].member.clear();
    }
  }
  return 0;
}