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

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  int i, j, k;
  while (cin >> n, n) {
    map<string, vector<string>> m;
    map<string, bool> b;
    string s, key, f;
    for (i = 0; i < n; i++) {
      cin >> s;
      for (j = 0; j < s.size(); j++)
        if (s[j] == ':' || s[j] == ',' || s[j] == '.')
          s[j] = ' ';
      stringstream ss(s);
      ss >> key;
      if (i == 0)
        f = key;
      while (ss >> s)
        m[key].push_back(s);
    }
    vector<string> d(m[f]);
    for (i = 0; i < d.size(); i++) {
      // cout << d[i] << endl;
      if (m[d[i]].size() == 0)
        continue;
      if (b[d[i]]) {
        d.erase(d.begin() + i);
        i--;
        continue;
      }
      b[d[i]] = true;
      // cout << d[i] << endl;
      for (j = 0; j < m[d[i]].size(); j++) {
        d.push_back(m[d[i]][j]);
      }
      d.erase(d.begin() + i);
      i--;
    }

    sort(d.begin(), d.end());

    // for(i=0;i<d.size();i++) cout << d[i] << endl;

    d.erase(unique(d.begin(), d.end()), d.end());
    cout << d.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  int i, j, k;
  while (cin >> n, n) {
    map<string, vector<string>> m;
    string s, key, f;
    for (i = 0; i < n; i++) {
      cin >> s;
      for (j = 0; j < s.size(); j++)
        if (s[j] == ':' || s[j] == ',' || s[j] == '.')
          s[j] = ' ';
      stringstream ss(s);
      ss >> key;
      if (i == 0)
        f = key;
      while (ss >> s)
        m[key].push_back(s);
    }
    vector<string> d(m[f]);
    for (i = 0; i < d.size(); i++) {
      // cout << d[i] << endl;
      if (m[d[i]].size() == 0)
        continue;
      for (j = 0; j < m[d[i]].size(); j++) {
        d.push_back(m[d[i]][j]);
      }
      d.erase(d.begin() + i);
      i--;
    }

    sort(d.begin(), d.end());

    // for(i=0;i<d.size();i++) cout << d[i] << endl;

    d.erase(unique(d.begin(), d.end()), d.end());
    cout << d.size() << endl;
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#define F first
#define S second
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
map<string, int> index;
int dex;

void add(VVI &G, string s) {
  rep(i, s.size()) if (s[i] == ':' || s[i] == ',' || s[i] == '.') s[i] = ' ';
  stringstream ss;
  ss << s;
  int fir = -1;
  while (!(ss >> s).fail()) {
    if (!index[s])
      index[s] = dex++;
    int v = index[s];
    if (fir == -1)
      fir = v;
    else {
      assert(fir < 1100);
      G[fir].push_back(v);
    }
  }
}

int count(VVI &G, bool used[], int p) {

  if (used[p])
    return 0;
  used[p] = true;
  if (G[p].empty())
    return 1;
  int cnt = 0;
  rep(i, G[p].size()) cnt += count(G, used, G[p][i]);
  return cnt;
}

int main() {
  int n;
  while (cin >> n, n) {
    dex = 1;
    index.clear();
    VVI G(1100);
    rep(i, n) {
      string s;
      cin >> s;
      add(G, s);
    }

    bool used[dex];
    rep(i, dex) used[i] = false;
    cout << count(G, used, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#define F first
#define S second
using namespace std;
map<string, vector<string>> G;
map<string, bool> visited;
string st;
int cnt;

void split(int now) {
  string line;
  cin >> line;
  for (int i = 0; i < line.size(); i++)
    if (line[i] == ':' || line[i] == ',' || line[i] == '.')
      line[i] = ' ';

  stringstream ss;
  ss << line;
  string Get, name;

  for (int i = 0; i < ss.str().size(); i++) {
    if ((ss >> Get).fail())
      break;
    if (now == 0 && i == 0)
      st = Get;
    if (i == 0) {
      name = Get;
      continue;
    }
    G[name].push_back(Get);
  }
}

void dfs(string now) {

  for (int i = 0; i < G[now].size(); i++) {
    if (!visited[G[now][i]] && G.find(G[now][i]) == G.end())
      cnt++;
    visited[G[now][i]] = true;
    if (G.find(G[now][i]) != G.end())
      dfs(G[now][i]);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    G.clear();
    visited.clear();

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

    cnt = 0;
    visited[st] = true;
    dfs(st);
    cout << cnt << endl;
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int vy[] = {-1, 1, 0, 0}, vx[] = {0, 0, 1, -1};

map<string, set<string>> names;
set<string> visited;
set<string> rec(string name) {
  set<string> ret;
  if (visited.find(name) != visited.end())
    return (ret);
  visited.insert(name);
  if (names[name].empty()) {
    ret.insert(name);
  } else {
    for (auto p : names[name]) {
      set<string> pp = rec(p);
      if (pp.size() > ret.size())
        swap(pp, ret);
      while (!pp.empty()) {
        ret.insert(*pp.begin());
        pp.erase(pp.begin());
      }
    }
  }
  return (ret);
}

int main() {
  int N;
  while (cin >> N, N) {
    string F = "";
    while (N--) {
      string S;
      cin >> S;
      string C = "";
      vector<string> buff;
      for (int i = 0; i < S.size(); i++) {
        if (isalpha(S[i]))
          C += S[i];
        else
          buff.push_back(C), C = "";
      }
      for (int i = 1; i < buff.size(); i++) {
        names[buff[0]].insert(buff[i]);
      }
      if (F.empty())
        F = buff[0];
    }
    cout << rec(F).size() << endl;
    visited.clear();
    names.clear();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int vy[] = {-1, 1, 0, 0}, vx[] = {0, 0, 1, -1};

map<string, set<string>> names;

set<string> rec(string name) {
  set<string> ret;
  if (names[name].empty()) {
    ret.insert(name);
  } else {
    for (auto p : names[name]) {
      set<string> pp = rec(p);
      if (pp.size() > ret.size())
        swap(pp, ret);
      while (!pp.empty()) {
        ret.insert(*pp.begin());
        pp.erase(pp.begin());
      }
    }
  }
  return (ret);
}

int main() {
  int N;
  while (cin >> N, N) {
    string F = "";
    while (N--) {
      string S;
      cin >> S;
      string C = "";
      vector<string> buff;
      for (int i = 0; i < S.size(); i++) {
        if (isalpha(S[i]))
          C += S[i];
        else
          buff.push_back(C), C = "";
      }
      for (int i = 1; i < buff.size(); i++) {
        names[buff[0]].insert(buff[i]);
      }
      if (F.empty())
        F = buff[0];
    }
    cout << rec(F).size() << endl;
    names.clear();
  }
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define N 100
using namespace std;

int main() {
  int n, s;
  string str[N];
  vector<string> v[N];
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      s = 0;
      for (int j = 0; j < str[i].size(); j++)
        if (str[i][j] == ':' || str[i][j] == ',' || str[i][j] == '.')
          v[i].push_back(str[i].substr(s, j - s)), s = j + 1;
    }
    while (1) {
      int flag = 1;
      for (int i = 0; i < n; i++) {
        if (!v[i].size())
          continue;
        if (i == 0 || !v[0].size())
          continue;
        for (int j = 1; j < v[0].size(); j++)
          if (v[i][0] == v[0][j]) {
            flag = 0;
            v[0].erase(v[0].begin() + j);
            for (int l = 1; l < v[i].size(); l++)
              v[0].push_back(v[i][l]);
          }
        sort(v[0].begin() + 1, v[0].end());
        for (int j = 1; j < v[0].size() - 1; j++) {
          if (v[0][j] == v[0][j + 1]) {
            v[0].erase(v[0].begin() + j);
            j--;
          }
        }
      }
      if (flag)
        break;
    }
    v[0].erase(v[0].begin());
    cout << v[0].size() << endl;
    for (int i = 0; i < n; i++)
      v[i].clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define N 100
using namespace std;

int main() {
  int n, s;
  string str[N];
  vector<string> v[N];
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      s = 0;
      for (int j = 0; j < str[i].size(); j++)
        if (str[i][j] == ':' || str[i][j] == ',' || str[i][j] == '.')
          v[i].push_back(str[i].substr(s, j - s)), s = j + 1;
    }
    while (1) {
      int flag = 1;
      for (int i = 0; i < n; i++) {
        if (!v[i].size())
          continue;
        if (i == 0 || !v[0].size())
          continue;
        for (int k = 1; k < v[0].size(); k++) {
          if (v[i][0] == v[0][k]) {
            flag = 0;
            v[0].erase(v[0].begin() + k);
            for (int l = 1; l < v[i].size(); l++)
              v[0].push_back(v[i][l]);
          }
        }
      }
      if (flag)
        break;
    }
    v[0].erase(v[0].begin());
    sort(v[0].begin(), v[0].end());
    for (int j = 0; j < v[0].size() - 1; j++) {
      if (v[0][j] == v[0][j + 1]) {
        v[0].erase(v[0].begin() + j);
        j--;
      }
    }
    cout << v[0].size() << endl;
    for (int i = 0; i < n; i++)
      v[i].clear();
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    map<string, set<string>> groups;
    set<string> gs;
    string group1;
    for (int i = 0; i < n; i++) {
      string line;
      cin >> line;
      size_t pos = line.find_first_of(':');
      string group = line.substr(0, pos);
      line.erase(0, pos + 1);
      if (i == 0) {
        group1 = group;
      }
      gs.insert(group);
      set<string> members;
      while (pos = line.find_first_of(",."), pos != string::npos) {
        members.insert(line.substr(0, pos));
        line.erase(0, pos + 1);
      }
      groups[group] = members;
    }
    while (true) {
      set<string> members = groups[group1];
      queue<string> q;
      for (set<string>::iterator it = members.begin(); it != members.end();
           it++) {
        set<string>::iterator jt = gs.find(*it);
        if (jt != gs.end()) {
          q.push(*jt);
        }
      }
      if (q.empty()) {
        break;
      }
      while (!q.empty()) {
        string g = q.front();
        q.pop();
        groups[group1].erase(g);
        set<string> m = groups[g];
        for (set<string>::iterator it = m.begin(); it != m.end(); it++) {
          groups[group1].insert(*it);
        }
      }
    }
    cout << groups[group1].size() << endl;
  }
}

Error: TLE

Fixed submission:

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

map<string, vector<string>> groups;

string doesInclude(set<string> s) {
  for (set<string>::iterator it = s.begin(); it != s.end(); it++) {
    map<string, vector<string>>::iterator i;
    if ((i = groups.find(*it)) != groups.end()) {
      return i->first;
    }
  }
  return "";
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }
    groups.clear();
    string first;
    set<string> results;
    for (int i = 0; i < n; i++) {
      string line;
      cin >> line;
      string group = line.substr(0, line.find(':'));
      line.erase(0, line.find(':') + 1);
      vector<string> members;
      size_t pos;
      while ((pos = line.find_first_of(",.")) != string::npos) {
        members.push_back(line.substr(0, pos));
        line.erase(0, pos + 1);
      }
      groups.insert(make_pair(group, members));
      if (i == 0) {
        for (vector<string>::iterator it = members.begin(); it != members.end();
             it++) {
          results.insert(*it);
        }
      }
    }
    string target;
    while ((target = doesInclude(results)) != "") {
      results.erase(target);
      for (vector<string>::iterator it = groups[target].begin();
           it != groups[target].end(); it++) {
        results.insert(*it);
      }
    }
    cout << results.size() << endl;
  }
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)

typedef set<string> ss;

map<string, ss> memo;

ss recursive(ss groupname, map<string, ss> group, string gname) {
  if (memo.find(gname) != memo.end())
    return memo[gname];

  ss ret;
  for (string name : group[gname]) {
    if (groupname.find(name) != groupname.end()) {
      ss tmp = recursive(groupname, group, name);
      for (string s : tmp)
        ret.insert(s);
    } else {
      ret.insert(name);
    }
  }
  return (memo[gname] = ret);
}

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

    memo.clear();

    ss groupname;
    map<string, ss> group;
    string first_groupname;

    rep(i, 0, n) {
      string s;
      cin >> s;
      string gname;
      int index;

      index = s.find(":");
      gname = s.substr(0, index);
      groupname.insert(gname);
      group[gname] = ss();

      if (i == 0)
        first_groupname = gname;

      s = s.substr(index + 1);

      while ((index = s.find(",")) != string::npos) {
        string name = s.substr(0, index);
        group[gname].insert(name);
        s = s.substr(index + 1);
      }

      index = s.find(".");
      string name = s.substr(0, index);
      group[gname].insert(name);
    }

    ss ans = recursive(groupname, group, first_groupname);
    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)

typedef set<string> ss;

ss recursive(ss groupname, map<string, ss> group, string gname) {
  ss ret;
  for (string name : group[gname]) {
    if (groupname.find(name) != groupname.end()) {
      ss tmp = recursive(groupname, group, name);
      for (string s : tmp)
        ret.insert(s);
    } else {
      ret.insert(name);
    }
  }
  return ret;
}

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

    ss groupname;
    map<string, ss> group;
    string first_groupname;

    rep(i, 0, n) {
      string s;
      cin >> s;
      string gname;
      int index;

      index = s.find(":");
      gname = s.substr(0, index);
      groupname.insert(gname);
      group[gname] = ss();

      if (i == 0)
        first_groupname = gname;

      s = s.substr(index + 1);

      while ((index = s.find(",")) != string::npos) {
        string name = s.substr(0, index);
        group[gname].insert(name);
        s = s.substr(index + 1);
      }

      index = s.find(".");
      string name = s.substr(0, index);
      group[gname].insert(name);
    }

    ss ans = recursive(groupname, group, first_groupname);
    cout << ans.size() << endl;
  }
}
Problem p00884 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

set<string> res;
map<string, vector<string>> mmset;
map<string, bool> visited;

int N;
char buf[20];
int main() {
  string fst = "";
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    res.clear();
    mmset.clear();
    visited.clear();
    for (int i = 0; i < N; i++) {
      int p = 0;
      char buf1;
      while ((buf1 = getchar()) && buf1 != ':') {
        buf[p++] = buf1;
      }
      string t = string(buf, p);
      //             cout << "header: " << t << "!" << endl;
      if (i == 0)
        fst = t;
      char inc;
      p = 0;
      while ((inc = getchar()) && inc != '\\n' && inc != '\\r') {
        if (inc == ',' || inc == '.') {
          string tt = string(buf, p);
          mmset[t].push_back(tt);
          //                     cout<<"found: "<<tt<<endl;
          p = 0;
        } else {
          buf[p++] = inc;
        }
      }
    }
    queue<string> workingset;
    workingset.push(fst);
    while (!workingset.empty()) {
      string pivot = workingset.front();
      workingset.pop();
      if (visited[pivot])
        continue;
      if (mmset.find(pivot) == mmset.end()) {
        res.insert(pivot);
        visited[pivot] = true;
      } else {
        for (auto i = mmset[pivot].begin(); i != mmset[pivot].end(); i++) {
          workingset.push(*i);
        }
        visited[pivot] = true;
      }
    }
    printf("%d\\n", res.size());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define lson l, mid, i << 1
#define rson mid + 1, r, i << 1 | 1
#define PII pair<int, int>
#define Size(x) (int)(x.size())
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const auto INF = 0x3f3f3f3f;
const int N = 2e3 + 5;

char str[N], ch[20];
map<string, int> maps;

vector<int> E[N], ans;

void dfs(int u, int fa) {
  if (!Size(E[u])) {
    ans.push_back(u);
    return;
  }
  for (int i = 0; i < Size(E[u]); i++) {
    int v = E[u][i];
    if (v == fa)
      continue;
    dfs(v, u);
  }
}

int main() {
  int n;
  while (~scanf("%d", &n) && n) {
    maps.clear();
    int cnt = 0, totall = 0;
    for (int i = 1; i <= 2000; i++)
      E[i].clear();
    for (int i = 1; i <= n; i++) {
      scanf("%s", str);
      int len = (int)strlen(str), id = -1;
      for (int j = 0; j < len; j++) {
        if (!(str[j] >= 'a' && str[j] <= 'z')) {
          ch[cnt] = '\\0';
          cnt = 0;
          if (maps.find(ch) == maps.end()) {
            maps[ch] = ++totall;
          }
          int fx = maps[ch];
          if (id == -1) {
            id = fx;
            continue;
          } else {
            E[id].push_back(fx);
          }
          continue;
        }
        ch[cnt++] = str[j];
      }
    }
    ans.clear();
    dfs(1, -1);
    sort(ans.begin(), ans.end());
    cnt = (int)(unique(ans.begin(), ans.end()) - ans.begin());
    printf("%d\\n", cnt);
  }
}
Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

map<string, set<string>> res;
set<string> te;
map<string, int> OK;

set<string> dfs(const string &s) {
  if (OK[s]) {
    return res[s];
  }
  OK[s] = 1;
  auto tmp = res[s];
  for (auto t : tmp) {
    if (te.count(t) == 0) {
      continue;
    }
    res[s].erase(t);
    auto ss = dfs(t);
    res[s].insert(ss.begin(), ss.end());
  }
  return res[s];
}

int main() {
  int n;
  while (cin >> n, n) {
    string S;
    vector<string> t(n);
    for (int j = 0; j < n; j++) {
      cin >> S;
      int tmp = 0;
      for (int i = 0; i < (int)S.size(); i++) {
        if (S[i] == ':') {
          te.insert(S.substr(0, i));
          t[j] = S.substr(0, i);
          tmp = i + 1;
        } else if (S[i] == ',' || S[i] == '.') {
          res[t[j]].insert(S.substr(tmp, i - tmp));
          tmp = i + 1;
        }
      }
    }
    cout << dfs(t[0]).size() << endl;
    res.clear();
    te.clear();
    OK.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int vis[1000];
map<string, int> ma;
int Sum = 0;
int Vis;
string s;
set<int> se;
vector<int> v[1000];
int tt;
char ch[100000];
void find(int x) {
  int i, j;
  if (vis[x] == 0)
    return;
  for (i = 0; i < v[x].size(); i++) {
    if (!vis[v[x][i]])
      se.insert(v[x][i]);
    else
      find(v[x][i]);
  }
}
int main() {
  int n, i, j;

  while (cin >> n && n) {
    ma.clear();
    memset(ch, 0, sizeof(ch));
    memset(vis, 0, sizeof(vis));
    Vis = 0;
    se.clear();
    for (i = 0; i < 1000; i++)
      v[i].clear();
    tt = 1;
    for (j = 0; j < n; j++) {
      scanf("%s", ch);
      int len = strlen(ch);
      int flag = 1;
      for (i = 0; i < len; i++) {
        if (ch[i] != ':' && ch[i] != ',' && ch[i] != '.')
          s += ch[i];
        else if (ch[i] == ':' && j == 0) {
          if (!ma[s]) {
            ma[s] = tt++;
          }
          Vis = ma[s];
          vis[ma[s]] = 1;
          flag = ma[s];
          s.clear();
        } else if (ch[i] == ':' && j != 0) {
          if (!ma[s]) {
            ma[s] = tt++;
          }
          vis[ma[s]] = 1;
          flag = ma[s];
          s.clear();
        } else if (ch[i] == ',' || ch[i] == '.') {
          if (!ma[s]) {
            ma[s] = tt++;
          }
          v[flag].push_back(ma[s]);
          s.clear();
        }
      }
    }
    Sum = 0;
    find(Vis);
    cout << se.size() << endl;
  }
  return 0;
}
Problem p00884 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  map<string, vector<string>> M;
  map<string, bool> Mp;
  int n;
  string s;
  string f;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    M.clear();
    Mp.clear();
    for (int i = 0; i < n; i++) {
      cin >> s;
      vector<string> V;
      int start = 0;
      for (int j = 0; j < s.length(); j++) {
        if (s[j] == ':' || s[j] == ',' || s[j] == '.') {
          V.push_back(s.substr(start, j - start));
          start = j + 1;
        }
        if (s[j] == ':' && i == 0)
          f = s.substr(0, j);
      }
      s = V[0];
      V.erase(V.begin());
      M[s] = V;
    }

    queue<string> Q;
    set<string> set;
    Q.push(f);
    Mp[f] = true;
    while (!Q.empty()) {
      string S = Q.front();
      Q.pop();
      vector<string> V = M[S];

      for (int i = 0; i < V.size(); i++) {
        if (M[V[i]].empty())
          set.insert(V[i]);
        else {
          if (Mp[V[i]])
            continue;
          Mp[V[i]] = true;
          Q.push(V[i]);
        }
      }
    }
    cout << set.size() << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int main() {
  map<string, vector<string>> M;
  int n;
  string s;
  string f;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    M.clear();

    for (int i = 0; i < n; i++) {
      cin >> s;
      vector<string> V;
      int start = 0;
      for (int j = 0; j < s.length(); j++) {
        if (s[j] == ':' || s[j] == ',' || s[j] == '.') {
          V.push_back(s.substr(start, j - start));
          start = j + 1;
        }
        if (s[j] == ':' && i == 0)
          f = s.substr(0, j);
      }
      s = V[0];
      V.erase(V.begin());
      M[s] = V;
    }

    queue<string> Q;
    set<string> set;
    Q.push(f);
    while (!Q.empty()) {
      string S = Q.front();
      Q.pop();
      vector<string> V = M[S];

      for (int i = 0; i < V.size(); i++) {
        if (M[V[i]].empty())
          set.insert(V[i]);
        else
          Q.push(V[i]);
      }
    }
    cout << set.size() << endl;
  }
  return 0;
}