1540 Legend of the Galactic Heroes
2002 NOI National Competition
time limit: 1 sspace limit: 256000 KBtopic rank: Master Master SolvingView Run ResultsTitle Description
Description
In 5801 A.D., the inhabitants of the Earth migrated to Taurus α the 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 battle, he divides the Battle of the &hellip into 30000 columns, numbered 1, 2, and 30000 in each column. He then numbered his battleship 1, 2, &hellip, and 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. |
Category labels
Tags Click here to expandAnd check the tree structure of the mainland Region Noi national Competition 2002
See:http://www.cnblogs.com/shenben/p/5757004.html
It's almost exactly the same question.
AC Code:
#include <cstdio>#include<cstdlib>#include<iostream>using namespacestd;#defineN 30010intFa[n],cnt[n],top[n];inlineintRead () {registerintx=0, f=1; RegisterCharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlineChar inch(){ for(RegisterCharCh=getchar (); Ch=getchar ())if(ch>='A'&&ch<='Z')returnch;}intFindintx) { if(fa[x]==x)returnx; intt=Fa[x]; FA[X]=find (fa[x]); FA[X]=Fa[t]; TOP[X]=Top[t]; CNT[X]=cnt[t]+Cnt[x]; returnfa[x];}intMain () {intN,x,y,a,b;Charch; N=read (); for(intI=1; i<=30000; i++) fa[i]=top[i]=i; for(intI=1; i<=n;i++) {ch=inch(); if(ch=='M') {a=read (); b=read (); X=find (a); y=find (b); FA[X]=Y;find (Top[y]); CNT[X]=cnt[top[y]]+1; Top[y]=Top[x]; } Else{a=read (); b=read (); X=find (a); y=find (b); if(X!=y) puts ("-1"); Elseprintf"%d\n", ABS (Cnt[a]-cnt[b])-1); } } return 0;}
1540 Legend of the Galactic heroes