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

Corporative Network

Corporative Network

A very big corporation is developing its corporative network. In the beginning each of the \textbf{N} enterprises of the corporation, numerated from \textbf{1} to \textbf{N}, organized its own computing and telecommunication center. Soon, for amelioration of the services, the corporation started to collect some enterprises in clusters, each of them served by a single computing and telecommunication center as follow. The corporation chose one of the existing centers \textbf{I} (serving the cluster \textbf{A}) and one of the enterprises \textbf{J} in some other cluster \textbf{B} (not necessarily the center) and link them with telecommunication line. The length of the line between the enterprises \textbf{I} and \textbf{J} is \textbf{|I -- J|(mod 1000)}. In such a way the two old clusters are joined in a new cluster, served by the center of the old cluster \textbf{B}. Unfortunately after each join the sum of the lengths of the lines linking an enterprise to its serving center could be changed and the end users would like to know what is the new length. Write a program to keep trace of the changes in the organization of the network that is able in each moment to answer the questions of the users. \InputFile Your program has to be ready to solve more than one test case. The first line of the input will contains only the number \textbf{T} of the test cases. Each test will start with the number \textbf{N} of enterprises (\textbf{5} ≤ \textbf{N} ≤ \textbf{20000}). Then some number of lines (no more than \textbf{200000}) will follow with one of the commands: \begin{itemize} \item \textbf{E I} - asking the length of the path from the enterprise \textbf{I} to its serving center in the moment; \item \textbf{I I J} - informing that the serving center \textbf{I} is linked to the enterprise \textbf{J}. \end{itemize} The test case finishes with a line containing the word \textbf{O}. The \textbf{I} commands are less than \textbf{N}. \OutputFile The output should contain as many lines as the number of \textbf{E} commands in all test cases with a single number each - the asked sum of length of lines connecting the corresponding enterprise with its serving center.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
1
4
E 3
I 3 1
E 3
I 1 2
E 3
I 2 4
E 3
O
Выходные данные #1
0
2
3
5
Источник SEERC 2004