Uvalive-3027-corporative Network (and check the set!!) )

Source: Internet
Author: User

UVALive-3027Corporative Network
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

A very BIG Corporation is developing its corporative network. In the beginning each of the N Enterprises of the corporation, numerated from 1 to N, organized its own computing and tele Communication Center. Soon, for Amelioration of the services, the corporation started to collect some enterprises in clusters, each of them serv Ed by a single computing and telecommunication Center as follow. The corporation chose one of the existing centers I (serving the cluster A) and one of the enterprises J in some cluster B (not necessarily the center) and link them with telecommunication line. The length of the line between the enterprises I and J are | I? j| (mod 1000). In such a, the clusters is joined in a new cluster, and served by the center of the old cluster B. Unfortunately a fter each join the sum of the lengths of the lines linking a enterprise to their serving center could be changed and the En D 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 are able in each moment to answer the questions of the users.

Input

Your program have to is ready-to-solve more than one test case. The first line of the input file would contains only the number T of the the test cases. Each test would start with the number N of enterprises (5≤n≤20000). Then some number of lines (no more than 200000) would follow with one of the commands:
E I? Asking the length of the path from the Enterprise I to its serving center in the moment;
I i J? Informing that the serving center I was linked to the enterprise J.
The test case is finishes with a line containing the word O. The I commands is less than N.

Output

The output should contain as many lines as the number of 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.

Sample Input

14E 3I 3 1E 3I 1 2E 3I 2 4E 3O

Sample Output

0235

Source

Regionals 2004 >> Europe-southeastern



Yesterday did not know how to do, the site crashed a day.

And check the set!!



AC Code:








Uvalive-3027-corporative Network (and check the set!!) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.