#P5833. Cow Milking Order
Cow Milking Order
Cow Milking Order
Farmer John has 8 cows with the following names: \(Beatrice\), \(Belinda\), \(Bella\), \(Bessie\), \(Betsy\), \(Blue\), \(Buttercup\), and \(Sue\). Every day, he must milk them in a specific order which satisfies \(N\) restrictions. Each restriction is of the form: "X must be adjacent to Y", meaning cow \(X\) must appear immediately before or immediately after cow \(Y\) in the milking order.
Your task is to determine an order that meets all the restrictions. It is guaranteed that there is at least one valid order. If multiple valid orders exist, output the lexicographically smallest order. That is, when comparing two orders, the order whose first differing cow name is lexicographically smaller (using standard alphabetical order) is chosen.
Note: Lexicographical order is determined by comparing the names as strings (e.g., "Beatrice" < "Belinda" < "Bella" < "Bessie" < "Betsy" < "Blue" < "Buttercup" < "Sue").
inputFormat
The first line contains an integer \(N\) (number of restrictions). Each of the following \(N\) lines contains two space-separated cow names \(X\) and \(Y\), representing that cow \(X\) must be adjacent to cow \(Y\) in the milking order.
outputFormat
Output a single line containing the 8 cow names in the valid milking order separated by spaces, which is lexicographically smallest among all valid orders.
sample
0
Beatrice Belinda Bella Bessie Betsy Blue Buttercup Sue