Codevs 1540 Legend of the Galactic heroes

Source: Internet
Author: User

Title Description Description

In 5801 A.D., the inhabitants of the Earth migrated to the Taurus Alpha Second Planet, where they published the Galactic Federation Declaration, which reign title the first year of the cosmic calendar and began to expand deep into the Milky Way.

In the 799-year universe, the two largest military groups in the galaxy erupted in the Battle of the Basque star. Taishanyading Group sent the Universe fleet Commander Reinhart led more than 100,000 warships to battle, Majestic Group point name Yang Weili organized 30,000 warships to meet the enemy.

Yang Weili good at formations, skillfully use various tactics repeatedly beat, inevitably pleases birth overbearing. In this decisive battle, he divides the Battle of the Fermi field into 30000 columns, numbered 1, 2, ..., and 30000 in each column. After that, he numbered his battleship 1, 2, ..., 30000, so that battleship I was in column I (i = 1, 2, ..., 30000), forming "Higo", lure. This is the initial pattern. When the enemy arrives, Yang Weili will release the merger instructions several times, concentrating most of the warships on a few columns and carrying out intensive attacks. The merge instruction is M i j, meaning that the entire battleship queue where battleship I is located, as a whole (head at the front end in the rear) is connected to the tail of the battleship queue where battleship J is located. Obviously the battleship queue is made up of one or more warships in the same column. The execution result of the merge directive increases the queue.

However, the wily Reinhart has already made a strategic initiative. During the war, he was able to monitor Yang Weili's fleet transfer instructions at any time through a vast intelligence network.

While Yang Weili issued instructions to mobilize the fleet, Reinhart also issued a number of inquiry instructions to keep abreast of the current distribution of Yang Weili warships: C i J. The directive means asking the computer if the Yang Weili battleship I and USS J are currently in the same column, and if they are in the same column, how many warships are arranged between them.

As a senior senior programmer, you are asked to write a program to analyze Yang Weili's instructions, and to answer the questions of Eli Hart.

The final battle has begun, and the history of the Milky Way has turned over a page ...

Enter a description Input Description

The first line of the input file galaxy.in has an integer t (1<=t<=500,000), which indicates that there is a total of t instructions.

There are t-lines below, with one instruction per line. Directives are available in two formats:

1. M I j:i and J are two integers (1<=i, j<=30000), indicating the battleship number involved in the instruction. The directive is a fleet transfer order issued by Reinhart eavesdropping on Yang Weili and guarantees that battleship I is not in the same column as Battleship J.

2. C I j:i and J are two integers (1<=i, j<=30000), indicating the battleship number involved in the instruction. This directive is an inquiry instruction issued by Reinhart.

Output description Output Description

The output file is galaxy.out. Your program should analyze and process each of the input commands in turn:

If it is a fleet transfer order issued by Yang Weili, it means that the fleet alignment has changed, and your program should be aware of this, but do not export any information;

If it is a Reinhart issued query instruction, your program will output a line containing only an integer representing the number of warships arranged between Battleship I and Battleship J on the same column. If battleship No. I is not currently on the same column as battleship J, output-1.

Sample input Sample Input

4

M 2 3

C 1 2

M 2 4

C 4 2

Sample output Sample Output

-1

1

Data range and Tips Data Size & Hint

First column

Second column

Third column

Fourth column

......

Initial time

1

2

3

4

......

M 2 3

1

3

2

4

......

C 1 2

Battleship 1th is not in the same column as battleship 2nd, so output-1

M 2 4

1

4

3

2

......

C 4 2

There is only one battleship between battleship 4th and battleship 2nd, numbered 3, Output 1.

"Problem-solving ideas"

I always think this topic is particularly pompous, because the topic is so long but there is no egg use, it comes down to the problem is the application of the collection

Defines three arrays f,s,sum,f represents the root, s represents the position of the current item in the collection, sum represents the number of the number under the current root node

1 varN,i,f1,f2,a,b:longint;2 Ch:char;3Sum,f,s:Array[0..30000] ofLongint;4 functionGetfather (v:longint): Longint;5 varP:longint;6 begin7   ifF[v]=v Thenexit (v);8p:=Getfather (F[v]);9s[v]:=s[f[v]]+s[v];//change location When path is compressedTenf[v]:=p;//Path Compression Onegetfather:=F[v]; A End; - begin -Assign (input,'galaxy.in'); theAssign (output,'Galaxy.out'); - reset (input); - rewrite (output); - READLN (n); +    fori:=1  to 30000  Do begins[i]:=0; sum[i]:=1; f[i]:=i;End;//Initialize -    fori:=1  toN Do +     begin A read (CH); at Readln (A, b); -f1:=Getfather (a); -f2:=Getfather (b); -       ifCh='M'  Then -         begin -s[f1]:=sum[f2]+S[F1]; insum[f2]:=sum[f2]+SUM[F1]; -f[f1]:=F2; to         End; +       ifCh='C'  Then -         ifF1<>f2 ThenWriteln (-1)ElseWriteln (ABS (S[A]-S[B))-1); the     End; * close (input); $ close (output);Panax Notoginseng End.

Codevs 1540 Legend of the Galactic heroes

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.