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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef complex<double> P;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EPS 1.0e-9

// AOJ1305
set<string> ss;
multimap<string, string> mss;
set<string> vis;

int main() {
  int n;
  char buf[1024];
  while (cin >> n, n) {
    ss.clear();
    mss.clear();
    vis.clear();
    cin >> buf;
    char *_c = strtok(buf, ":");
    while ((_c = strtok(NULL, ",.")) != NULL)
      ss.insert(_c);
    REP(i, n - 1) {
      cin >> buf;
      char *c = strtok(buf, ":");
      string tstr(c);
      mss.insert(MP(tstr, "0"));
      while ((c = strtok(NULL, ",.")) != NULL)
        mss.insert(MP(tstr, c));
    }
    int flag = 1;
    do {
      flag = 0;
      for (set<string>::iterator it = ss.begin(); it != ss.end(); it++) {
        pair<multimap<string, string>::iterator,
             multimap<string, string>::iterator>
            its = mss.equal_range(*it);
        if (its.first == its.second)
          continue;
        its.first++;
        ss.erase(it);
        for (multimap<string, string>::iterator it2 = its.first;
             it2 != its.second; it2++) {
          ss.insert(it2->second);
          flag = 1;
        }
        mss.erase(its.first, its.second);
        if (flag)
          break;
      }
    } while (flag);
    cout << ss.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef complex<double> P;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EPS 1.0e-9

// AOJ1305
set<string> ss;
multimap<string, string> mss;

int main() {
  int n;
  char buf[1024];
  while (cin >> n, n) {
    ss.clear();
    mss.clear();
    cin >> buf;
    char *_c = strtok(buf, ":");
    while ((_c = strtok(NULL, ",.")) != NULL)
      ss.insert(_c);
    REP(i, n - 1) {
      cin >> buf;
      char *c = strtok(buf, ":");
      string tstr(c);
      while ((c = strtok(NULL, ",.")) != NULL)
        mss.insert(MP(tstr, c));
    }
    int flag = 1;
    do {
      flag = 0;
      for (set<string>::iterator it = ss.begin(); it != ss.end(); it++) {
        pair<multimap<string, string>::iterator,
             multimap<string, string>::iterator>
            its = mss.equal_range(*it);
        if (its.first != its.second) {
          ss.erase(it);
          flag = 1;
          for (multimap<string, string>::iterator it2 = its.first;
               it2 != its.second; it2++)
            ss.insert(it2->second);
          break;
        }
      }
    } while (flag);
    cout << ss.size() << endl;
  }
  return 0;
}