National Olympiad in Informatics, China, 2002
In the year 2801 CE, the Earth's inhabitants have migrated to Theoria, the second planet of the Aldebaran Starzone in the Taurus constellation. There and then, the Galactic Federation was officially founded. That same year, the Common Era (CE) calendar was replaced by the Universal Calendar (UC) to commemorate this establishment. Subsequently, further expansion into the depths of the galaxy commenced.
In the year 799 UC, battle erupted between the two largest military forces of the galaxy in the Vermilion Starzone. The Galactic Empire ordered commander Reinhard von Lohengramm to take a fleet of roughly one hundred thousand warships to demolish the thirty thousand opposing ships under the command of admiral Yang Wen-li.
Yang Wen-li specializes in directing soldiers and battle formations. In
the past, he has won countless battles using brilliant tactics,
especially when grossly outnumbered. Inevitably, his pride grows with
every battle. During this ultimate battle, he has divided the
battlefield of Vermilion into M i j
, indicating that the entire fleet in the column
containing ship
However, the cunning Reinhard had long been a step ahead. At any time during the battle, he will be able to use a vast intelligence network to listen in on Yang Wen-li's commands for directing his fleet.
While Yang Wen-li is issuing a command to transfer his fleets, some
queries will be issued in the format C i j
. This command asks the
computer: do Yang Wen-li's ship
As an experienced senior programmer, you have been requested to write a program that analyzes Yang Wen-li's orders, as well as answers Reinhard's queries.
The final battle has begun, thus starting a new chapter in the galaxy's history.
Input Specification
The first line of input contains a single integer
M i j
: where and are two integers , representing the numbers of the ships involved. These are commands issued by Yang Wen-li and secretly obtained by commander Reinhard. It is guaranteed that ship and ship are not already in the same column.C i j
: where and are two integers , representing the numbers of the ships involved. These are queries made by Reinhard about the status of Yang Wen-li's fleet.
Output Specification
You are to analyze and process the commands in the input. For each of
Yang Wen-li's commands that indicate a merge of fleets, your program
should take note of the change but not output any information. For each
of Reinhard's queries, your program must output one line containing a
single integer. If ships -1
.
Sample Input
4
M 2 3
C 1 2
M 2 4
C 4 2
Sample Output
-1
1
Explanation
Here is an outline of the ship locations throughout the battle:
Column 1 | Column 2 | Column 3 | Column 4 | ... | |
---|---|---|---|---|---|
Initial |
|
|
|
|
... |
M 2 3 |
|
|
|
... | |
C 1 2 |
Ship number | ||||
M 2 4 |
|
|
... | ||
C 4 2 |
There is a single ship (ship |
Problem translated to English by .
Comments