#P5833. [USACO19DEC] Livestock Lineup B

[USACO19DEC] Livestock Lineup B

Problem Description

Every day, Farmer John milks his 88 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 NN constraints. Each constraint is of the form "XX must be milked beside YY", meaning cow XX must be immediately after cow YY in the milking order, or immediately before cow YY.

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 NN. The next NN lines each contain a sentence describing a constraint in the format "XX must be milked beside YY", where XX and YY are names of Farmer John's cows (from the eight possible names listed above).

Output Format

Output a cow order in 88 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

1N71 \leq N \leq 7.

Problem by: Brian Dean.

Translated by ChatGPT 5