VijosP1443: Legend of the Galactic heroes

Source: Internet
Author: User

Describe

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 ...

Format input Format

The first line of input 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 format

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.

Input:

4
M 2 3
C 1 2
M 2 4
C 4 2

Output:

1

-1

Idea: See Code

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=30005;structnode{intpar; intHead//indicates how many ships are in front of I    intTail//There are several ships in the back when I is the root .}NODES[MAXN];intT;voidPrep () { for(intI=0; i<maxn;i++) {Nodes[i].par=i; Nodes[i].head=0; Nodes[i].tail=0; }}intFndintx) {    if(x==Nodes[x].par)returnx; intFa=nodes[x].par;//Record parent node firstNODES[X].PAR=FND (Nodes[x].par);//The parent node of all nodes becomes the root nodeNodes[x].head+=nodes[fa].head;//Update parent node first, and then update child nodes    returnNodes[x].par;}voidUniteintXinty) {    intA=fnd (x); intb=fnd (y); if(a!=b) {Nodes[a].head= (nodes[b].tail+1); Nodes[b].tail+ = (nodes[a].tail+1); Nodes[a].par=b; }}intQueryintXinty) {    intA=fnd (x); intb=fnd (y); if(a==b)returnABS (Nodes[x].head-nodes[y].head)-1; Else            return-1;}intMain () {scanf ("%d",&T);    Prep ();  while(t--) {scanf ("%*c"); Charop; intx, y; scanf ("%c%d%d",&op,&x,&y); if(op=='M') {unite (x, y); }        Else{printf ("%d\n", query (x, y)); }    }        return 0;} 

VijosP1443: 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.