All problems

Problem 943

Find All Possible Recipes from Given Supplies

Medium · 20 pts · Graph, Topological Sort, Hash Table

Problem

You have some supplies in unlimited amounts and a list of recipes. Each recipe needs certain ingredients, and an ingredient can be a supply or another recipe. Find every recipe you can make, and print them in the order they appear in the input, or NONE. Treat it as a topological sort: a recipe becomes makeable once all its ingredients are available, and making it may unlock others. Recipes caught in a cycle can never be made.

Input

The first line contains R. Each of the next R lines contains a recipe name, a count K and its K ingredients. The next line contains S, and the last line contains the S supplies.

Output

Print the makeable recipes in input order, separated by spaces, or NONE.

Constraints

1 <= R <= 100; 1 <= K <= 100; 1 <= S <= 100; all names are lowercase words; recipe names are distinct and differ from supply names

Example input

2
bread 2 yeast flour
sandwich 2 bread meat
3
yeast flour meat

Example output

bread sandwich

Explanation

With supplies yeast, flour and meat, bread (yeast, flour) can be made, and then sandwich (bread, meat) too. So the answer is bread sandwich.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Largest Color Value in a Directed Graph Next: Maximum Employees to Be Invited to a Meeting