from typing import List, Optional, Tuple, Dict, Set
from collections import defaultdict, Counter, deque
import heapq
import math
import bisect
import itertools
import functools


class Solution:
    def findAllRecipes(
        self, recepies: List[str], ingredients: List[List[str]], supplies: List[str]
    ) -> List[str]:
        dct = defaultdict(lambda: [])
        indegree = {}
        n = len(recepies)

        for i in recepies:
            indegree[i] = 0

        for i in range(n):
            for j in ingredients[i]:
                indegree[j] = 0

        for i in range(n):
            for j in ingredients[i]:
                dct[j].append(recepies[i])
                indegree[recepies[i]] += 1

        st = []
        for i in indegree:
            if indegree[i] == 0:
                st.append(i)
        flst = []
        ans = defaultdict(lambda: [])
        while st:
            x = st.pop(0)
            for i in dct[x]:
                # if ans[x]!=[]:
                for j in ans[x]:
                    if j not in ans[i]:
                        ans[i].append(j)
                ans[i].append(x)
                indegree[i] -= 1
                if indegree[i] == 0:
                    st.append(i)
            if x in recepies:
                for k in ans[x]:
                    if k not in supplies:
                        break
                else:
                    flst.append(x)
                    supplies.append(i)

        return flst
