alexjercan/bugnet huggingface dataset:
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
char a[111111], b[111111], *p;
char buf[222222];
map<string, bool> used;
set<string> res;
void solve(string s, map<string, vector<string>> &g) {
  if (used[s])
    return;
  used[s] = true;
  vector<string> &w = g[s];
  for (int i = 0; i < int(w.size()); i++) {
    if (g[w[i]].size() != 0)
      solve(w[i], g);
    else
      res.insert(w[i]);
  }
}
int main(void) {
  int n;
  for (;;) {
    scanf("%d", &n);
    if (!n)
      break;
    res.clear();
    used.clear();
    vector<string> v;
    map<string, vector<string>> g;
    for (int i = 0; i < n; i++) {
      scanf("%s", buf);
      sscanf(buf, "%[^:]:%[^.]", a, b);
      v.push_back(a);
      // printf("a:%s %d\\n",a,strlen(a));
      p = strtok(b, ",");
      vector<string> &r = g[a];
      while (p) {
        r.push_back(p);
        // printf("%s %d\\n",p,strlen(p));
        p = strtok(NULL, ",");
      }
    }
    solve(v[0], g);
    printf("%d\\n", res.size());
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
char a[1111], b[11111], *p;
set<string> res;
void solve(string s, map<string, vector<string>> &g) {
  vector<string> &w = g[s];
  for (int i = 0; i < int(w.size()); i++) {
    if (g[w[i]].size() != 0)
      solve(w[i], g);
    else
      res.insert(w[i]);
  }
}
int main(void) {
  int n;
  for (;;) {
    scanf("%d", &n);
    if (!n)
      break;
    res.clear();
    vector<string> v;
    map<string, vector<string>> g;
    for (int i = 0; i < n; i++) {
      char buf[22222];
      scanf("%s", buf);
      sscanf(buf, "%[^:]:%[^.]", a, b);
      v.push_back(a);
      // printf("a:%s %d\\n",a,strlen(a));
      p = strtok(b, ",");
      vector<string> &r = g[a];
      while (p) {
        r.push_back(p);
        // printf("%s %d\\n",p,strlen(p));
        p = strtok(NULL, ",");
      }
    }
    solve(v[0], g);
    printf("%d\\n", res.size());
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n;
  for (; cin >> n, n;) {
    map<string, int> hash;
    vector<string> team[n];

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      // teamname
      int j = 0;
      for (; s[j] != ':'; j++)
        ;
      string name = s.substr(0, j);
      hash[name] = i;
      name = "";
      j++;
      for (; j < s.size(); j++) {
        if (s[j] == ',' || s[j] == '.') {
          team[i].push_back(name);
          //  cout<<"in "<<name<<endl;
          name = "";
        } else
          name += s[j];
      }
    }
    int cnt = 0;
    queue<int> qu;
    map<string, bool> mem;
    qu.push(0);
    while (!qu.empty()) {
      int num = qu.front();
      qu.pop();
      for (int j = 0; j < team[num].size(); j++) {
        if (mem.count(team[num][j]) == 0) {
          if (hash.count(team[num][j]) != 0)
            qu.push(hash[team[num][j]]);
          else
            cnt++;
          mem[team[num][j]] = true;
        }
      }
    }

    cout << cnt << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n;
  for (; cin >> n, n;) {
    map<string, int> hash;
    vector<string> team[n];

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      // teamname
      int j = 0;
      for (; s[j] != ':'; j++)
        ;
      string name = s.substr(0, j);
      hash[name] = i;
      name = "";
      j++;
      for (; j < s.size(); j++) {
        if (s[j] == ',' || s[j] == '.') {
          team[i].push_back(name);
          //  cout<<"in "<<name<<endl;
          name = "";
        } else
          name += s[j];
      }
    }
    int cnt = 0;
    queue<int> qu;
    map<string, bool> mem;
    qu.push(0);
    while (!qu.empty()) {
      int num = qu.front();
      qu.pop();
      for (int j = 0; j < team[num].size(); j++) {
        if (hash.count(team[num][j]) != 0) {
          qu.push(hash[team[num][j]]);
        } else if (mem.count(team[num][j]) == 0) {
          cnt++;
        }
        mem[team[num][j]] = true;
      }
    }

    cout << cnt << endl;
  }
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

set<string> &
num_of_member(string const &name, unordered_map<string, set<string>> &member_dp,
              unordered_map<string, vector<string>> const &member) {
  if (member_dp.find(name) != member_dp.end()) {
    return member_dp[name];
  }
  set<string> ret;
  auto it = member.find(name);
  if (it == member.end()) {
    ret.insert(name);
  } else {
    for (auto const &n : it->second) {
      auto v = num_of_member(n, member_dp, member);
      set_union(ret.begin(), ret.end(), v.begin(), v.end(),
                inserter(ret, ret.end()));
      // ret.insert(v.begin(), v.end());
    }
  }
  member_dp[name] = ret;
  return member_dp[name];
}

void solve(int n) {
  unordered_map<string, vector<string>> member;
  string line, first;
  for (int i = 0; i < n; i++) {
    cin >> line;
    auto colon = line.find(':');
    auto name = line.substr(0, colon);
    if (i == 0) {
      first = name;
    }
    auto start = colon + 1;
    bool end = false;
    while (!end) {
      auto comma = line.find(',', start);
      if (comma == string::npos) {
        comma = line.size() - 1;
        end = true;
      }
      auto mem_name = line.substr(start, comma - start);
      member[name].push_back(mem_name);
      start = comma + 1;
    }
  }
  unordered_map<string, set<string>> member_dp;
  cout << num_of_member(first, member_dp, member).size() << endl;
}

int main() {
  int n;
  while (cin >> n && n) {
    solve(n);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

set<string> num_of_member(string const &name,
                          unordered_map<string, vector<string>> const &member) {
  set<string> ret;
  auto it = member.find(name);
  if (it == member.end()) {
    ret.insert(name);
  } else {
    for (auto const &n : it->second) {
      auto v = num_of_member(n, member);
      set_union(ret.begin(), ret.end(), v.begin(), v.end(),
                inserter(ret, ret.end()));
      // ret.insert(v.begin(), v.end());
    }
  }
  return ret;
}

void solve(int n) {
  unordered_map<string, vector<string>> member;
  string line, first;
  for (int i = 0; i < n; i++) {
    cin >> line;
    auto colon = line.find(':');
    auto name = line.substr(0, colon);
    if (i == 0) {
      first = name;
    }
    auto start = colon + 1;
    bool end = false;
    while (!end) {
      auto comma = line.find(',', start);
      if (comma == string::npos) {
        comma = line.size() - 1;
        end = true;
      }
      auto mem_name = line.substr(start, comma - start);
      member[name].push_back(mem_name);
      start = comma + 1;
    }
  }
  cout << num_of_member(first, member).size() << endl;
}

int main() {
  int n;
  while (cin >> n && n) {
    solve(n);
  }
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

unordered_map<string, set<string>> memo;
set<string> f(map<string, string> &mp, const string &g) {
  if (memo.count(g))
    return memo[g];
  set<string> ret;
  if (mp.count(g) == 0) {
    ret.insert(g);
  } else {
    string ms = mp[g];
    string member = "";
    rep(i, (int)(ms.size())) {
      if (ms[i] == ',' || ms[i] == '.') {
        auto retret = f(mp, member);
        for (auto e : retret)
          ret.insert(e);
        member = "";
      } else {
        member += ms[i];
      }
    }
  }
  return memo[g] = ret;
}

int main() {

  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    memo.clear();
    map<string, string> mp;
    vector<string> gs;
    string root = "";
    rep(i, n) {
      string s;
      cin >> s;
      int _i = (int)s.find(":");
      string group = s.substr(0, _i);
      if (i == 0)
        root = group;
      gs.push_back(group);
      mp[group] = s.substr(_i + 1);
    }
    auto ret = f(mp, root);
    cout << ret.size() << endl;
    // for(auto s: ret) cout<< s<< endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

set<string> f(map<string, string> mp, const string &g) {
  set<string> ret;
  if (mp.count(g) == 0) {
    ret.insert(g);
  } else {
    string ms = mp[g];
    string member = "";
    rep(i, (int)(ms.size())) {
      if (ms[i] == ',' || ms[i] == '.') {
        auto retret = f(mp, member);
        for (auto e : retret)
          ret.insert(e);
        member = "";
      } else {
        member += ms[i];
      }
    }
  }
  return ret;
}

int main() {

  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    map<string, string> mp;
    vector<string> gs;
    string root = "";
    rep(i, n) {
      string s;
      cin >> s;
      int _i = (int)s.find(":");
      string group = s.substr(0, _i);
      if (i == 0)
        root = group;
      gs.push_back(group);
      mp[group] = s.substr(_i + 1);
    }
    auto ret = f(mp, root);
    cout << ret.size() << endl;
    // for(auto s: ret) cout<< s<< endl;
  }

  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()

using data = map<string, int>;

void add(data &d, string s) {
  if (d.find(s) == d.end()) {
    int n = d.size();
    d[s] = n;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    data mp;
    vector<vector<string>> input;
    set<int> k;
    k.insert(0);
    rep(i, n) {
      input.emplace_back();
      string s;
      cin >> s;
      s = regex_replace(s, regex(":|,"), " ");
      stringstream ss(s);
      ss >> s;
      add(mp, s);
      while (ss >> s) {
        if (s.back() == '.')
          s = s.substr(0, s.size() - 1);
        input[i].push_back(s);
      }
    }
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
      int f = q.front();
      q.pop();
      rep(i, input[f].size()) {
        if (mp.find(input[f][i]) != mp.end()) {
          if (k.find(mp[input[f][i]]) == k.end()) {
            k.insert(mp[input[f][i]]);
            q.push(mp[input[f][i]]);
          }
        }
      }
    }
    set<string> ans;
    for (auto i : k) {
      rep(j, input[i].size()) {
        if (mp.find(input[i][j]) == mp.end())
          ans.insert(input[i][j]);
      }
    }
    cout << ans.size() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, a, n) for (int i = a; i < n; i++)
#define all(a) a.begin(), a.end()

using data = map<string, int>;

void add(data &d, string s) {
  if (d.find(s) == d.end()) {
    int n = d.size();
    d[s] = n;
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    data mp;
    vector<vector<string>> input;
    set<int> k;
    rep(i, n) {
      input.emplace_back();
      string s;
      cin >> s;
      s = regex_replace(s, regex(":|,"), " ");
      stringstream ss(s);
      ss >> s;
      add(mp, s);
      while (ss >> s) {
        if (s.back() == '.')
          s = s.substr(0, s.size() - 1);
        input[i].push_back(s);
      }
    }
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
      int f = q.front();
      q.pop();
      k.insert(f);
      rep(i, input[f].size()) {
        if (mp.find(input[f][i]) != mp.end()) {
          if (k.find(mp[input[f][i]]) == k.end())
            q.push(mp[input[f][i]]);
        }
      }
    }
    set<string> ans;
    for (auto i : k) {
      rep(j, input[i].size()) {
        if (mp.find(input[i][j]) == mp.end())
          ans.insert(input[i][j]);
      }
    }
    cout << ans.size() << endl;
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, vector<string>> grp;
map<string, bool> prn;
vector<string> split(string str) {
  vector<string> vec;
  string s;

  for (int i = 0; i < str.size(); i++) {
    s = "";
    while (i < str.size() && isalpha(str[i]))
      s += str[i++];

    if (s.size())
      vec.push_back(s);
  }

  return vec;
}

int search(string tg) {
  int ln = 0;
  vector<string> vec = grp[tg];
  prn[tg] = true;

  for (int i = 0; i < vec.size(); i++) {
    if (!prn[vec[i]] && grp.find(vec[i]) != grp.end())
      ln += search(vec[i]);
    else if (!prn[vec[i]]) {
      ln++;
      prn[vec[i]] = true;
    }
  }

  return ln;
}

int main() {
  int n;
  string str, tg;

  while (cin >> n && n) {
    bool f = true;
    while (n--) {
      cin >> str;
      vector<string> vec = split(str);
      str = vec[0];
      vec.erase(vec.begin());
      grp[str] = vec;

      if (f) {
        tg = str;
        f = false;
      }
    }

    cout << search(tg) << endl;
    grp.clear();
    prn.clear();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, vector<string>> grp;
map<string, bool> prn;
vector<string> split(string str) {
  vector<string> vec;
  string s;

  for (int i = 0; i < str.size(); i++) {
    s = "";
    while (i < str.size() && isalpha(str[i]))
      s += str[i++];

    if (s.size())
      vec.push_back(s);
  }

  return vec;
}

int search(string tg) {
  int ln = 0;
  vector<string> vec = grp[tg];
  prn[tg] = false;

  for (int i = 0; i < vec.size(); i++) {
    if (!prn[vec[i]] && grp.find(vec[i]) != grp.end())
      ln += search(vec[i]);
    else if (!prn[vec[i]]) {
      ln++;
      prn[vec[i]] = true;
    }
  }

  return ln;
}

int main() {
  int n;
  string str, tg;

  while (cin >> n && n) {
    bool f = true;
    while (n--) {
      cin >> str;
      vector<string> vec = split(str);
      str = vec[0];
      vec.erase(vec.begin());
      grp[str] = vec;

      if (f) {
        tg = str;
        f = false;
      }
    }

    cout << search(tg) << endl;
    grp.clear();
    prn.clear();
  }

  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
vector<string> group;
vector<vector<string>> member;
set<string> ret;

void solve(string s) {
  int k = find(group.begin(), group.end(), s) - group.begin();
  if (k == n) {
    ret.insert(s);
    return;
  }

  for (unsigned i = 0; i < member[k].size(); ++i)
    solve(member[k][i]);
  member[k].clear();
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;

    group.assign(n, "");
    member.assign(n, vector<string>(0));

    for (int i = 0; i < n; ++i) {
      char c;
      for (;;) {
        cin >> c;
        if (c == ':')
          break;
        group[i] += c;
      }
      string s;
      for (;;) {
        cin >> c;
        if (c == '.') {
          member[i].push_back(s);
          break;
        }
        if (c == ',') {
          member[i].push_back(s);
          s.clear();
        } else {
          s += c;
        }
      }
    }

    ret.clear();
    solve(group[0]);
    cout << ret.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n;
vector<string> group;
vector<vector<string>> member;
set<string> ret;

void solve(string s) {
  int k = find(group.begin(), group.end(), s) - group.begin();
  if (k == n) {
    ret.insert(s);
    return;
  }

  for (unsigned i = 0; i < member[k].size(); ++i)
    solve(member[k][i]);
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;

    group.assign(n, "");
    member.assign(n, vector<string>(0));

    for (int i = 0; i < n; ++i) {
      char c;
      for (;;) {
        cin >> c;
        if (c == ':')
          break;
        group[i] += c;
      }
      string s;
      for (;;) {
        cin >> c;
        if (c == '.') {
          member[i].push_back(s);
          break;
        }
        if (c == ',') {
          member[i].push_back(s);
          s.clear();
        } else {
          s += c;
        }
      }
    }

    ret.clear();
    solve(group[0]);
    cout << ret.size() << endl;
  }
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

map<string, vector<string>> mp;
map<string, set<string>> memo;

vector<string> split(string s, char ch) {
  vector<string> ret;
  string tmp = "";
  rep(i, s.size()) {
    if (s[i] == ch) {
      ret.pb(tmp);
      tmp = "";
    } else
      tmp += s[i];
  }
  if (tmp.size())
    ret.pb(tmp);
  return ret;
}

set<string> solve(string s) {
  if (memo.count(s) != 0)
    return memo[s];

  vector<string> members = mp[s];
  set<string> ret;
  rep(i, members.size()) {
    if (mp.count(members[i]) == 0)
      ret.insert(members[i]);
    else {
      set<string> tmp = solve(members[i]);
      for (auto &elm : tmp)
        ret.insert(elm);
    }
  }
  memo[s] = ret;
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    mp.clear();
    memo.clear();
    string group1;
    rep(i, n) {
      string s;
      cin >> s;
      vector<string> vs = split(s, ':');
      assert(vs.size() == 2);
      vector<string> members = split(vs[1].substr(0, vs[1].size() - 1), ',');
      mp[vs[0]] = members;
      if (i == 0)
        group1 = vs[0];
    }
    set<string> res = solve(group1);
    cout << res.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

map<string, vector<string>> mp;
map<string, set<string>> memo;

vector<string> split(string s, char ch) {
  vector<string> ret;
  string tmp = "";
  rep(i, s.size()) {
    if (s[i] == ch) {
      ret.pb(tmp);
      tmp = "";
    } else
      tmp += s[i];
  }
  if (tmp.size())
    ret.pb(tmp);
  return ret;
}

set<string> solve(string s) {
  if (memo.count(s) != 0)
    return memo[s];

  vector<string> members = mp[s];
  set<string> ret;
  rep(i, members.size()) {
    if (mp.count(members[i]) == 0)
      ret.insert(members[i]);
    else {
      set<string> tmp = solve(members[i]);
      for (auto &elm : tmp)
        ret.insert(elm);
    }
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    string group1;
    rep(i, n) {
      string s;
      cin >> s;
      vector<string> vs = split(s, ':');
      assert(vs.size() == 2);
      vector<string> members = split(vs[1].substr(0, vs[1].size() - 1), ',');
      mp[vs[0]] = members;
      if (i == 0)
        group1 = vs[0];
    }
    set<string> res = solve(group1);
    cout << res.size() << endl;
  }
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

void pushset(std::map<std::string, std::vector<std::string>> *mp,
             std::set<std::string> *ans, std::string name,
             std::set<std::string> *group, std::map<std::string, int> *f) {
  using namespace std;

  if (group->find(name) != group->end()) {
    if (f->at(name))
      return;
    f->at(name) = 1;
    for (int i = 0; i < mp->at(name).size(); i++) {
      pushset(mp, ans, mp->at(name)[i], group, f);
    }
  } else {
    ans->insert(name);
  }
}

int solve() {
  using namespace std;

  int n;
  cin >> n;
  if (n == 0)
    return 1;

  vector<string> ss(n);
  map<string, vector<string>> mp;
  set<string> group;
  map<string, int> flg;
  string top;

  for (int i = 0; i < n; i++) {

    char tstr[1000];
    char *tok;
    cin >> ss[i];

    for (int j = 0; j < ss[i].length(); j++)
      tstr[j] = ss[i][j];

    tstr[ss[i].length()] = '\\0';
    tok = strtok(tstr, ":,.");

    string s;
    vector<string> st;
    for (int j = 0;; j++) {
      if (tok == NULL)
        break;
      string str = string(tok);
      if (j == 0) {
        s = str;
        if (i == 0)
          top = str;
        group.insert(str);
        flg[str] = 0;
      } else {
        st.push_back(str);
      }
      tok = strtok(NULL, ";,.");
    }

    mp[s] = st;
  }
  set<string> ans;
  for (int i = 0; i < mp[top].size(); i++) {
    pushset(&mp, &ans, mp[top][i], &group, &flg);
  }
  cout << ans.size() << endl;
  return 0;
}

int main(void) {

  while (true) {
    if (solve())
      break;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string.h>
#include <string>
#include <vector>

struct cmp {
  std::string name;
  std::vector<std::string> comp;
};

void push(std::vector<cmp> *v, std::set<std::string> *s, std::string name) {
  using namespace std;
  bool b = true;
  for (int i = 1; i < v->size(); i++) {
    if (v->at(i).name == name) {
      for (int j = 0; j < v->at(i).comp.size(); j++) {
        push(v, s, v->at(i).comp[j]);
      }
      b = false;
    }
  }
  if (b) {
    s->insert(name);
  }
}

int solve() {
  using namespace std;

  int n;
  cin >> n;
  if (n == 0)
    return 1;

  vector<string> ss(n);
  vector<cmp> cs(n);
  set<string> st;

  for (int i = 0; i < n; i++) {

    char tstr[1000];
    char *tok;
    cin >> ss[i];

    for (int j = 0; j < ss[i].length(); j++)
      tstr[j] = ss[i][j];

    tstr[ss[i].length()] = '\\0';
    tok = strtok(tstr, ":,.");

    for (int j = 0;; j++) {
      if (tok == NULL)
        break;
      string str = string(tok);
      if (j == 0) {
        cs[i].name = str;
      } else {
        cs[i].comp.push_back(str);
      }
      tok = strtok(NULL, ";,.");
    }
  }

  for (int i = 0; i < cs[0].comp.size(); i++)
    push(&cs, &st, cs[0].comp[i]);

  cout << st.size() << endl;
  return 0;
}

int main(void) {

  while (true) {
    if (solve())
      break;
  }
  return 0;
}