Problem p00884 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < n; ++i)
#define RREP(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define ALL(v) (v).begin(), (v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v)                                                                \
  REP(aa, (v).size()) {                                                        \
    cout << v[a];                                                              \
    if (a != v.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
map<string, int> mp;
set<string> cnt;
void add(int i, vi &p, vector<vector<string>> &v, vector<bool> &b) {
  if (b[i])
    return;
  set<string> tmp;
  REP(j, v[i].size()) {
    if (mp[v[i][j]] != 0) {
      int nx = mp[v[i][j]] - 1;
      add(nx, p, v, b);

    } else
      cnt.insert(v[i][j]);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n, n) {
    mp.clear();
    cnt.clear();
    vector<vector<string>> v(n);
    vi p(n);
    REP(i, n) {
      string s;
      cin >> s;

      s.back() = ',';
      REP(j, s.size()) {
        if (s[j] == ':') {
          p[i] = j;
          break;
        }
      }
      mp[s.substr(0, p[i])] = i + 1;
      string tmp;
      FOR(j, p[i] + 1, s.size()) {
        if (s[j] == ',') {
          v[i].push_back(tmp);
          tmp = "";
        } else
          tmp += s[j];
      }
    }
    vector<bool> b(n, false);
    add(0, p, v, b);
    cout << cnt.size() << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"

#define REP(i, n) for (ll i = 0; i < n; ++i)
#define RREP(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define RFOR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define ALL(v) (v).begin(), (v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define DUMP(v)                                                                \
  REP(aa, (v).size()) {                                                        \
    cout << v[a];                                                              \
    if (a != v.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
map<string, int> mp;
set<string> cnt;
void add(int i, vi &p, vector<vector<string>> &v, vector<bool> &b) {
  if (b[i])
    return;
  set<string> tmp;
  b[i] = true;
  REP(j, v[i].size()) {
    if (mp[v[i][j]] != 0) {
      int nx = mp[v[i][j]] - 1;
      add(nx, p, v, b);

    } else
      cnt.insert(v[i][j]);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n, n) {
    mp.clear();
    cnt.clear();
    vector<vector<string>> v(n);
    vi p(n);
    REP(i, n) {
      string s;
      cin >> s;

      s.back() = ',';
      REP(j, s.size()) {
        if (s[j] == ':') {
          p[i] = j;
          break;
        }
      }
      mp[s.substr(0, p[i])] = i + 1;
      string tmp;
      FOR(j, p[i] + 1, s.size()) {
        if (s[j] == ',') {
          v[i].push_back(tmp);
          tmp = "";
        } else
          tmp += s[j];
      }
    }
    vector<bool> b(n, false);
    add(0, p, v, b);
    cout << cnt.size() << endl;
  }
}