View the wonderful distance of the galaxy Warship

Source: Internet
Author: User

View the question to know that this is noi2002's Galaxy-Galaxy hero legend

The questions are as follows:

Legend of Galaxy hero description a year, Earth residents migrated to Taurus Alpha second planet, where he published a declaration of the creation of the galaxy Federation, the same year to change the yuan for the universe, and began to expand to the depths of the galaxy.
In the 799 s of the cosmic calendar, two military groups in the Milky Way broke out in the bamianne domain. Lainhart, the commander of the Taishan Summit group's cosmic fleet, led more than 100,000 warships to fly out, and Yang Weiwei, a star of the mountain river group, organized 30 thousand warships to welcome the enemy.
Yang Weiwei is good at arranging troops and skillfully uses various tactics to win more than once and for all, which inevitably leads to arrogance. In this decisive battle, he divided the battlefield of the bamianne domain into 30000 columns, with each column numbered 1, 2 ,..., 30000. Then, he numbers his warships as 1, 2 ,..., 30000. Place warship I in column I (I = 1, 2 ,..., 30000. This is the initial formation. When the enemy of the attacker arrives, Yang Willi will issue merge commands multiple times to concentrate most warships on certain columns for intensive attacks. The merging command is m I j, which means to make the entire warship queue where warship I is located as a whole (the first and last are behind) to the tail of the warship queue where warship J is located. Apparently, a warship queue is composed of one or more warships in the same column. The execution result of the merge command will increase the queue.
However, lainhart, who has been well-known, has already taken the initiative in strategy. During the battle, he can monitor Yang's fleet mobilization commands at any time through a large intelligence network.
While yang Willi Issued commands to mobilize the fleet, lainhart also issued some inquiry commands to learn about the distribution of the warships of Yang Willi in a timely manner: C I j. This command is used to ask the computer whether the warship I of Yang Wylie is in the same column as the warship J. If it is in the same column, how many warships are arranged between them.
As a Senior Programmer, you are asked to write a program to analyze Yang Huili's instructions and to answer a question from lainhart.
The final decisive battle has been launched, and the history of the galaxy has gone through another page ...... The first line of inputformat contains an integer T (1 <= T <= 500,000), indicating that a total of T commands exist.
There are t lines below, and each line has a command. There are two formats of commands:
1. m I j: I and j are two integers (1 <= I, j <= 30000), indicating the number of warships involved in the command. This command is a ship transfer command issued by Yang Huili, which was intercepted by lainhart, and ensures that warship I and warship J are not in the same column.
2. c I j: I and j are two integers (1 <= I, j <= 30000), indicating the warship number involved in the command. This command is a query command issued by lainhart.

Output Format outputformat your program should analyze and process each input command in sequence:
If it is the fleet transfer command issued by Yang Huili, it indicates that the fleet arrangement has changed. Your program should pay attention to this point, but do not output any information;
If the query command is issued by lainhart, your program will output a line containing only one integer, indicating the number of warships arranged between warship I and warship J on the same column. If warship I and warship J are not in the same column, output-1. Input4
M 2 3
C 1 2
M 2 4
C 4 2
Output-1
1 [question analysis] Well, the above is all nonsense. Now, let's analyze this question. It seems nothing special. It is just a collection query. However, it is not hard to find out that, the distance between the two warships in the inquiry is a headache. So can we get it by modifying and querying the set? The answer is yes. Now, we will introduce a variant of the query set-maintain distance and query the set. We can use an dis array to record the distance from each warship to the first warship in this column. In this way, we only need to make a difference when querying the distance between the two warships. So how should dis be maintained? First, in the initial state, the DIS of each warship is 0. When we connect this warship to another column, we only need to find the first column of the new warship from this warship along the way, in the process of finding a father, we can update the DIS continuously. Let's take a look at the code of this part of the find function ~
1 int find(int x)2 {3     int t;4     if(fa[x]==x)return x;5     t=fa[x];6     fa[x]=find(fa[x]);7     dis[x]+=dis[t];8     return fa[x];9 }

It is not hard to see that in the recursive process, we see the update of DIS. Every time we use t to record the father before path compression. Then we recursively operate T, that is, the current father. After calculating T, we calculate dis [x] + = dis [T] to implement layer-by-layer computing.

Finally, we only need to return the parent value after the path is compressed. That's simple, isn't it? I have been learning this article for a long time. I have been learning this article .... I am speechless with my IQ.

In addition, there are some small details, for example, we need to record the number of the team-end warship of each warship, it is easy to find the parent of the first warship in the next column when the warship columns are merged. They are all in the code. On the code, do not read the message ~ We will try our best to solve this problem before noip2014.
 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 int T; 7 int fa[30050],last[30050],dis[30050]; 8 int find(int x) 9 {10     int t;11     if(fa[x]==x)return x;12     t=fa[x];13     fa[x]=find(fa[x]);14     dis[x]+=dis[t];15     return fa[x];16 }17 int main()18 {19     for(int i=1;i<=30000;i++)20     {21         fa[i]=i;22         last[i]=i;23         dis[i]=0;24     }25     cin>>T;26     char cmd[5];27     int x,y;28     for(int i=1;i<=T;i++)29     {30         scanf("%s%d%d",&cmd,&x,&y);31         if(cmd[0]==‘M‘)32         {33             int f1=find(x);34             int f2=find(y);35             if(f1!=f2)36             {37                 fa[f1]=last[f2];38                 last[f2]=last[f1];39                 dis[f1]=1;40             }41         }42         else if(cmd[0]==‘C‘)43         {44             int f1=find(x);45             int f2=find(y);46             int ans=fabs(dis[x]-dis[y])-1;47             if(f1==f2)printf("%d\n",ans);48             else printf("-1\n");49         }50     }51     return 0;52 }
Galaxy. cpp

 

Note: DIS [] records the distance to the first warship. Last [] records the number of the team-end warship in the warship column. Fa. I believe you can easily understand it ~~

View the wonderful distance of the galaxy Warship

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.