#P5833. [USACO19DEC] Livestock Lineup B
[USACO19DEC] Livestock Lineup B
Problem Description
Every day, Farmer John milks his cows. Their names are Bessie, Buttercup, Belinda, Beatrice, Bella, Blue, Betsy, and Sue.
Unfortunately, these cows are quite hard to deal with. They require Farmer John to milk them in an order that satisfies constraints. Each constraint is of the form " must be milked beside ", meaning cow must be immediately after cow in the milking order, or immediately before cow .
Please help Farmer John find a milking order that satisfies all constraints. It is guaranteed that such an order exists. If there are multiple valid orders, output the one with the smallest lexicographical order. That is, the first cow must be the one with the smallest name in lexicographical order among all cows that can appear first in some valid milking order. Among all valid milking orders that start with this lexicographically smallest cow, the second cow must be lexicographically smallest, and so on.
Input Format
The first line contains . The next lines each contain a sentence describing a constraint in the format " must be milked beside ", where and are names of Farmer John's cows (from the eight possible names listed above).
Output Format
Output a cow order in lines, one cow name per line, satisfying all constraints. If multiple orders satisfy the requirements, output the lexicographically smallest cow order.
3
Buttercup must be milked beside Bella
Blue must be milked beside Bella
Sue must be milked beside Beatrice
Beatrice
Sue
Belinda
Bessie
Betsy
Blue
Bella
Buttercup
Hint
.
Problem by: Brian Dean.
Translated by ChatGPT 5