Data structure (and check set): 260. [NOI2002] Galactic Heroes Legend

Source: Internet
Author: User

260. [NOI2002] Legend of the Galactic heroes

★★☆ input file: galaxy.in output file: galaxy.out Simple comparison
Time limit: 5 s memory limit: MB

"Problem description"

     in 5801, the Earth's inhabitants 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.      799, the two largest military groups in the galaxy erupted in the Battle of the Basque planet. 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, clever use of 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 been strategically active. During the war, he was able to monitor Yang Weili's fleet transfer instructions at any time through a vast intelligence network.      at the same time Yang Weili issued instructions to mobilize the fleet, Reinhart in order to keep abreast of the current distribution of Yang Weili warships, will also issue some inquiry instructions: 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 ... input format input file the first line of 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.
The output format output file is galaxy.out. Your program should analyze and process each of the input commands in turn: If it is a fleet Transfer command issued by Yang Weili, it means that the fleet alignment has changed, your program should be aware of this, but do not output any information, and if it is a Reinhart issued query, your program will output a line containing only an integer. Represents 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 Output Sample input file name: Galaxy.in4
M 2 3
C 1 2
M 2 4
C 4 2 Output file name: galaxy.out-1
1

"Sample description"

Battleship Location Map: Arabic numerals in tables indicate battleship numbers

column, , column, column .
Firstsecond columnfourththree
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.

  

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=50010;6 intFA[MAXN],SZ[MAXN],RANK[MAXN];7 Charop[ -];8 intQ[maxn],tot;9 intFind (intXint&RT) {Tenrt=x;tot=0; One      while(fa[rt]!=RT) { Aq[++tot]=RT; -rt=Fa[rt]; -     } the     intsum=1; -      while(tot) { -sum+=rank[q[tot]]-1; -rank[q[tot]]=sum; +fa[q[tot]]=rt;tot--; -     } +     returnRank[x]; A } at intMain () { -Freopen ("galaxy.in","R", stdin); -Freopen ("Galaxy.out","W", stdout); -     intq,a,b; -scanf"%d",&Q); -      for(intI=1; i<=50000; i++) fa[i]=i,rank[i]=1, sz[i]=1; in      while(q--){ -scanf"%s", op); toscanf"%d%d",&a,&b); +         if(op[0]=='M'){ -             intRA,RB; the Find (A,ra); * Find (B,RB); $fa[ra]=RB;Panax Notoginsengrank[ra]=sz[rb]+1; -sz[rb]+=Sz[ra]; the         } +         Else{ A             intra,rb,la,lb; theLa=Find (A,ra); +lb=Find (B,RB); -             if(ra!=RB) $printf"-1\n"); $             Else -printf"%d\n", la>lb?la-lb-1: lb-la-1); -         } the     } -     return 0;Wuyi}

Data structure (and check set): 260. [NOI2002] Galactic Heroes Legend

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.