eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

VAN DINSKY

VAN DINSKY

Вы должны помочь будущему художнику Винсенту ван Динскому смешать цвета для своих картин. Учитель дал Винсенту книгу, содержащую правила смешения цветов, а также цветовую палитру, и попросил его получить набор цветов, необходимый для написании картины. Винсенту придется экспериментировать, пока он не найдет минимальное количество цветовых смесей, необходимых для получения каждого цвета в картине. \textit{\textbf{Примечания}}: \begin{itemize} \item все названия цветов используют символы \textbf{a-z 0-9} (нижний регистр) \item a color mixing rule is made by three colors written on the same line, meaning "\textbf{color1 mixed with color2 gives color3}" -- e.g. "\textbf{yellow cyan green}" \item mr. van Dinsky is not willing to do random color mixing experiments -- if he doesn’t find in the book what is the result of mixing yellow with green, he will never attempt to do this operation. He is also very bad at logic inference -- e.g. if the book says that \textbf{yellow+cyan=green}, \textbf{yellow+magenta=red} and \textbf{red+cyan=black}, he will not assume that \textbf{green+magenta=black} (unless the book explicitly tells what is the result of mixing \textbf{green & magenta}). On the other hand -- he is fully aware that it doesn’t matter what color you start with -- mixing \textbf{yellow & green} is the same as mixing \textbf{green & yellow}. \end{itemize} \InputFile Содержит данные в следующем порядке: \begin{itemize} \item все правила смешения цветов в книге \item пустая строка \item один или несколько тестов, каждый из которых состоит из двух строк: \begin{itemize} \item все доступные цвета изначально находятся на палитре \item все цвета, необходимые для написания картины \end{itemize} \end{itemize} \OutputFile You should produce an output that contains one line for each painting (i.e. each data set); on that line, for each color in the painting, the minimum number of color mixes that will produce the desired color (starting from the colors available initially on the palette; the result is '\textbf{-1}' if it’s impossible to obtain the desired color). You can assume that the input is correct, there are less than \textbf{50000} color mixing rules in the rule book and less than 1000 colors.
Лимит времени 5 секунд
Лимит использования памяти 256 MiB
Входные данные #1
cyan yellow green
cyan magenta blue
yellow magenta red
red green black

cyan yellow magenta pink
cyan black pink red brown
cyan yellow red
blue black
Выходные данные #1
0 3 0 1 -1
-1 2
Источник ACM ICPC SEERC-2012, 13.10.2012 Bucharest, Vinnica