Build schedule for a new hospital wing
A construction firm in Dubai is building a hospital wing. Some tasks cannot start before others are finished (the roof needs the walls, the walls need the foundation). The project manager works with one crew and wants one valid order for all tasks. To make the plan the same for everyone, whenever several tasks are ready at the same time the one whose name comes first alphabetically (ordinary string comparison) goes first.
Task: the first line holds n and m (1 ≤ n ≤ 60, 0 ≤ m ≤ 200). The second line holds the n distinct task names (letters and digits). Each of the next m lines holds a b, meaning task a must be finished before task b can start. Print the full order of tasks on one line separated by spaces. If the dependencies contain a cycle so that no valid order exists, print CYCLE.
Sample test
Input
6 6 roof walls foundation wiring paint plumbing foundation walls walls roof walls wiring walls plumbing wiring paint plumbing paint
Expected output
foundation walls plumbing roof wiring paint
Your answer is also checked against 5 hidden tests.
Need a hint?
Always keep the tasks with no unfinished prerequisites in a priority queue ordered by name.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
