Cookbook Composition
InterviewTime limit2sMemory limit2048 MB
Given each recipe as a dependency graph of timed steps, sort the recipes by the ratio of serial time to parallel critical-path time.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Topological sort, Sorting
- Solved
- No attempts yet
Problem
The world-famous chef Gordon Oliver is composing a new cookbook called "Becoming A Perfect Chef". He has a list of recipes to publish in it. Each recipe is a list of steps. A step can depend on earlier steps, so it cannot start until all of its dependencies have finished. Each step also has an expected time.
Gordon knows that, as an expert chef, he can multitask and do as many tasks simultaneously as needed. A beginner can do only one task at a time, so a beginner must execute the tasks sequentially. He wants to order the recipes by accessibility, with the lowest ratio first.
For example, take the first sample case. For the oven dish, an expert can prepare the tomatoes, eggplants, and sauce at the same time. The sauce takes the longest, 5, so these steps finish after time 5. Arranging (1) and baking (30) follow, for a total of . A beginner needs . The accessibility ratio of the oven dish is . The ice cream ratio is 1, because a beginner and an expert both need . So the ice cream comes before the oven dish.
Input
The first line has an integer (), the number of recipes.
Then, for every recipe:
One line with the recipe name and an integer (), the number of steps.
lines follow, one per step. Each line has the step name, an integer () for the step duration, an integer () for the number of dependencies, and then the names of those steps. A step appears only after all the steps it depends on have been listed.
Recipe and step names consist of at most 10 English lowercase letters (a-z). Recipe names are unique, and step names are unique within each recipe.
Output
Output the recipe names in the cookbook, ordered by accessibility. If there are multiple valid solutions, you may output any one of them.