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

// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// splitテゥツ鳴「テヲツ閉ー
// (1テヲツ鳴?・ツュツ療」ツδ静」ツδシテ」ツつクテ」ツδァテ」ツδウテ」ツ?づヲツ個?・ツョツ堙」ツ?陛」ツつ古」ツ?淌ヲツ鳴?・ツュツ療」ツ?ァテヲツ鳴?・ツュツ療・ツ按療」ツつ津・ツ按?・ツ可イ)
vector<string> split(string s, char c) {
  vector<string> ret;
  string temp;
  replace(s.begin(), s.end(), c, ' ');
  stringstream ss(s);
  while (ss >> temp)
    ret.push_back(temp);
  return ret;
}

bool visited[100010];
vector<int> G[100010];
map<string, int> m;
map<int, string> rev;
int id = 0;

int get_id(string x) {
  if (m.count(x))
    return m[x];
  else {
    m[x] = id;
    rev[id++] = x;
    return m[x];
  }
}

void add_edge(string x, string y) {
  int a = get_id(x), b = get_id(y);
  G[a].push_back(b);
}

void dfs(set<int> &S, int p, int par = -1) {
  bool leaf = true;
  for (auto to : G[p]) {
    if (to == par)
      continue;
    leaf = false;
    if (visited[to])
      continue;
    visited[to] = true;
    dfs(S, to, p);
  }
  if (leaf)
    S.insert(p);
}

signed main() {
  int N;
  while (cin >> N, N) {
    m.clear();
    memset(visited, false, sizeof(visited));
    rep(i, 0, 100010) G[i].clear();
    id = 0;

    rep(i, 0, N) {
      string s;
      cin >> s;
      vector<string> t = split(s, ':');
      t[1] = t[1].substr(0, (int)t[1].length() - 1);

      string from = t[0];
      vector<string> to = split(t[1], ',');
      rep(j, 0, to.size()) { add_edge(from, to[j]); }
    }

    set<int> S;
    dfs(S, 0);
    cout << S.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// テ・ツ淞コテヲツ慊ャテ」ツδ?」ツδウテ」ツδ療」ツδャテ」ツδシテ」ツδ?

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// splitテゥツ鳴「テヲツ閉ー
// (1テヲツ鳴?・ツュツ療」ツδ静」ツδシテ」ツつクテ」ツδァテ」ツδウテ」ツ?づヲツ個?・ツョツ堙」ツ?陛」ツつ古」ツ?淌ヲツ鳴?・ツュツ療」ツ?ァテヲツ鳴?・ツュツ療・ツ按療」ツつ津・ツ按?・ツ可イ)
vector<string> split(string s, char c) {
  vector<string> ret;
  string temp;
  replace(s.begin(), s.end(), c, ' ');
  stringstream ss(s);
  while (ss >> temp)
    ret.push_back(temp);
  return ret;
}

vector<int> G[100010];
map<string, int> m;
map<int, string> rev;
int id = 0;

int get_id(string x) {
  if (m.count(x))
    return m[x];
  else {
    m[x] = id;
    rev[id++] = x;
    return m[x];
  }
}

void add_edge(string x, string y) {
  int a = get_id(x), b = get_id(y);
  G[a].push_back(b);
}

void dfs(set<int> &S, int p, int par = -1) {
  bool leaf = true;
  for (auto to : G[p]) {
    if (to == par)
      continue;
    leaf = false;
    dfs(S, to, p);
  }
  if (leaf)
    S.insert(p);
}

signed main() {
  int N;
  while (cin >> N, N) {
    m.clear();
    rep(i, 0, 100010) G[i].clear();
    id = 0;

    rep(i, 0, N) {
      string s;
      cin >> s;
      vector<string> t = split(s, ':');
      t[1] = t[1].substr(0, (int)t[1].length() - 1);

      string from = t[0];
      vector<string> to = split(t[1], ',');
      rep(j, 0, to.size()) { add_edge(from, to[j]); }
    }

    set<int> S;
    dfs(S, 0);
    cout << S.size() << endl;
  }
  return 0;
}