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

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
int main() {
  int n;
  while (cin >> n, n) {
    map<string, vector<string>> mp;
    string st;
    rep(i, n) {
      string x;
      cin >> x;
      string ori;
      string nex;
      int len = x.length();
      bool f = false;
      rep(j, len) {
        if (x[j] == ':') {
          f = true;
        } else if (x[j] == ',' || x[j] == '.') {
          mp[ori].push_back(nex);
          nex.clear();
        } else {
          if (!f) {
            ori += x[j];
          } else {
            nex += x[j];
          }
        }
      }
      if (i == 0) {
        st = ori;
      }
    }
    vector<string> z;
    z.push_back(st);
    set<string> out;
    map<string, int> used;
    while (z.size()) {
      string now = z[0];
      z.erase(z.begin());
      int len = mp[now].size();
      rep(i, len) {
        if (mp[mp[now][i]].size()) {
          if (used[mp[now][i]] != 1) {
            z.push_back(mp[now][i]);
            used[mp[now][i]] = 1;
          }
        } else {
          out.insert(mp[now][i]);
        }
      }
    }
    int l = out.size();
    cout << l << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
const ld eps = 1e-8;
int main() {
  int n;
  while (cin >> n, n) {
    map<string, vector<string>> mp;
    string st;
    rep(i, n) {
      string x;
      cin >> x;
      string ori;
      string nex;
      int len = x.length();
      bool f = false;
      rep(j, len) {
        if (x[j] == ':') {
          f = true;
        } else if (x[j] == ',' || x[j] == '.') {
          mp[ori].push_back(nex);
          nex.clear();
        } else {
          if (!f) {
            ori += x[j];
          } else {
            nex += x[j];
          }
        }
      }
      if (i == 0) {
        st = ori;
      }
    }
    vector<string> z;
    z.push_back(st);
    set<string> out;
    while (z.size()) {
      string now = z[0];
      z.erase(z.begin());
      int len = mp[now].size();
      rep(i, len) {
        if (mp[mp[now][i]].size()) {
          z.push_back(mp[now][i]);
        } else {
          out.insert(mp[now][i]);
        }
      }
    }
    int l = out.size();
    cout << l << endl;
  }
  return 0;
}