#include<cstdio> #include<cstring> #include<string> #include<unordered_map> #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define N 1000 #define T 999000 usingnamespacestd;
structedge { int v, next; } E[T]; unordered_map<string, int> M; int adj[N]; int visited[N], low[N], step; int component; bool in_stack[N]; int Stack[N], top;
for (int i = adj[u]; i != -1; i = E[i].next) { int v = E[i].v; if (!visited[v]) DFS(v);
if (in_stack[v]) low[u] = MIN(low[u], low[v]); }
if (visited[u] == low[u]) { ++component; while (true) { int v = Stack[--top]; in_stack[v] = false; if (v == u) return; } } } intmain() { int P, t; char str[30]; while (scanf("%d%d", &P, &t) && (P || t)) { getchar(); for (int i = 0; i < P; ++i) { fgets(str, 30, stdin); M[string(str)] = i; }
memset(adj, -1, sizeof adj); int idx = 0; for (int i = 0; i < t; ++i) { int u, v; fgets(str, 30, stdin); u = M[string(str)]; fgets(str, 30, stdin); v = M[string(str)];
E[idx] = (edge){v, adj[u]}; adj[u] = idx++; }
for (int i = 0; i < P; ++i) if (!visited[i]) DFS(i);