Spectrum#6622

Spectrum

1 second
64 megabytes
Very hard

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 target is represented by a string of up to printing characters with no embedded spaces. A connection is a bi-directional relationship between two targets. The hop count between a given target (called "target1") and other targets is determined by the following rules:

  1. Targets directly connected to target1 are hops away.

  2. Targets directly connected to the hop targets, and not already counted as a hop target or the original target, are hop targets.

  3. Similarly, targets directly connected to hop targets, and not already counted in hop targets are n+1 hop targets.

There will be no more than targets and no more than connections.

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 columns. No leading or trailing whitespace is to appear on an output line.

add takes one or two parameters:

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)

add target1 target2 //two parameters

Function: Creates a bidirectional connection between the targets.

connections target1

Function: Returns the number of hops to direct and indirect connections from a target.

associated target1 target2

Function: Returns information about the existence of a connection between the two targets

Input

To allow for multiple test cases on the input file, we add a command "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 "reset" command at the end of the file.

Output

Start each case with a line consisting of "Case", one space, the case number, and a colon. End each case with a line of ten minus signs.

Examples

Example 1

Loading
Loading