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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

int n;

map<string, vector<string>> mss;
set<string> used;
set<string> ss;

void solve(string s) {
  if (mss.find(s) == mss.end()) {
    ss.insert(s);
    // cout << "!!" << ss.size() << endl;
    return;
  }
  rep(i, mss[s].size()) {
    if (used.find(mss[s][i]) == used.end())
      solve(mss[s][i]);
    used.insert(mss[s][i]);
  }
}

int main() {
  while (cin >> n && n) {
    used.clear();
    mss.clear();
    ss.clear();
    string s, ans;
    rep(i, n) {
      cin >> s;
      rep(j, s.size()) {
        if (s[j] == ':')
          s[j] = ',';
      }
      s.pop_back();
      vector<string> vs = split(s, ',');
      reverse(ALL(vs));
      string buf = vs.back();
      if (i == 0)
        ans = buf;
      vs.pop_back();
      mss[buf] = vs;
    }
    solve(ans);
    cout << ss.size() << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

int n;

map<string, vector<string>> mss;
set<string> ss;

void solve(string s) {
  if (mss.find(s) == mss.end()) {
    ss.insert(s);
    return;
  }
  rep(i, mss[s].size()) solve(mss[s][i]);
}

int main() {
  while (cin >> n && n) {
    mss.clear();
    ss.clear();
    string s, ans;
    rep(i, n) {
      cin >> s;
      rep(j, s.size()) {
        if (s[j] == ':')
          s[j] = ',';
      }
      s.pop_back();
      vector<string> vs = split(s, ',');
      reverse(ALL(vs));
      string buf = vs.back();
      if (i == 0)
        ans = buf;
      vs.pop_back();
      mss[buf] = vs;
    }
    solve(ans);
    cout << ss.size() << endl;
  }
}