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

Spectrum

Spectrum

Swamp County Consulting has just been awarded a contract from a mysterious government agency to build a database to investigate connections between what they call "targets." Your team has been sub-contracted to implement a system that stores target information and processes the commands listed below. A \textit{target} is represented by a string of up to \textbf{32} printing characters with no embedded spaces. A \textit{connection} is a bi-directional relationship between two targets. The \textit{hop count} between a given target (called "target1") and other targets is determined by the following rules: \begin{enumerate} \item Targets directly connected to target1 are \textbf{0} hops away. \item Targets directly connected to the \textbf{0} hop targets, and not already counted as a \textbf{0} hop target or the original target, are \textbf{1} hop targets. \item Similarly, targets directly connected to \textbf{n} hop targets, and not already counted in \textbf{0..n} hop targets are \textbf{n+1} hop targets. \end{enumerate} There will be no more than \textbf{100000} targets and no more than \textbf{500000} connections. \textbf{Commands} The data base system has only three commands: add, associated, and connections. Targets and connections are never deleted because the Agency never forgets or makes mistakes. Commands start in the first column of a line. Commands and their parameters are separated by whitespace. No input line will exceed \textbf{80} columns. No leading or trailing whitespace is to appear on an output line. \textit{add} takes one or two parameters: \textbf{add target1} //single parameter Function: Adds the target to the database, with no connections. Note: If target is already in the database, do nothing (not an error) \textbf{add target1 target2} //two parameters Function: Creates a bidirectional connection between the targets. \begin{itemize} \item If either target is not yet in the database, add it/them, and create the connection. \item If there is already a connection between the targets, do nothing (not an error) \item If target1 and target2 are the same, treat this as if the command were "add target1" (not an error) There can be at most one direct connection between targets. \end{itemize} \textbf{connections target1} Function: Returns the number of hops to direct and indirect connections from a target. \begin{itemize} \item Print the hop count, a colon, a single space, and the number of targets with that hop count with no leading zeroes on a separate line for each hop count. Start with hop count \textbf{0} and end with the hop count for the last non-zero number of targets. Do not print trailing spaces. \item If the target has no connections, print a line containing only the string "no connections". \item If the target is not in the database, print a line containing only the string "target does not exist" (no period). \end{itemize} \textbf{associated target1 target2} Function: Returns information about the existence of a connection between the two targets \begin{itemize} \item If there is a path between the targets, print "\textbf{yes: n}" on a separate line, where n is the hop count of target2 with respect to target1. There is one space after the colon and no leading zeroes and no trailing spaces. \item If there is no connection between the targets, print "\textbf{no}" on a separate line. \item If either target1 or target2 is not in the database, print a line containing only the string "\textbf{target does not exist}". \end{itemize} \InputFile To allow for multiple test cases on the input file, we add a command "\textbf{reset}" that is not a database command and occurs between the database commands for the different cases on the input file. Be sure to reset all of your data structures when you read this command. Process until end of file; there is no end-of-data flag and no "\textbf{reset}" command at the end of the file. \OutputFile Start each case with a line consisting of "\textbf{Case}", one space, the case number, and a colon. End each case with a line of ten minus signs.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
add a b
add a c
add b d
add e b
add c f
add c g
add f h
add h i
add j k
associated a i
associated i a
associated f k
associated a h
connections a
connections i
add k g
associated a j
connections a
add h a
connections a
associated a h
add m
add n n
connections n
add a n
connections n
Выходные данные #1
Case 1:
yes: 3
yes: 3
no
yes: 2
0: 2
1: 4
2: 1
3: 1
0: 1
1: 1
2: 1
3: 2
4: 1
5: 2
yes: 3
0: 2
1: 4
2: 2
3: 2
0: 3
1: 5
2: 1
3: 1
yes: 0
no connections
0: 1
1: 3
2: 5
3: 1
4: 1
----------
Источник ACM ICPC NCNA Programming Contest - November 7, 2013