Legend of the Galactic Heroes NOI 2002

P1196 [NOI2002] Legend of the Galactic Heroes Los Valley

Luo Gu Portal

1521 JDOJ: Legend of the Galactic Heroes VIJOS-P1443

JDOJ Portal

Description

AD Wuba ○ year, the Earth's inhabitants migrated to the second planet Taurus α, published the Declaration of the Galactic Federation was founded there in the same year reign title for the calendar year of the universe, and began to expand to the depths of the galaxy. Cosmic calendar 1799, two military blocs in the Milky Way star field Bami Leen war. Group pressurizing school universe Admiral Reinhardt led than one hundred thousand ships set off, full of daring group named Yang Weili organization will meet the enemy warships under the command of thirty thousand. Yang Weili good formations, frequently clever use of a variety of tactics to win with fewer, inevitably pleases raw arrogance. In this battle, he will Bami Lean star field battlefield is divided into 30,000 columns, each numbered 1, 2, ..., 30,000. After that, he put his warships are also numbered 1, 2, ..., 30000, No. i let warships in the i-th row (i = 1, 2, ..., 30000), to form a "word Hydra array", luring the enemy in depth. This is the initial lineup. When the invading enemy arrives, Yang Weili will merge issued several instructions, most of the ships focus on a few columns, the implementation of intensive attacks. Merge instruction for the M ij, meaning the whole ship queue where the i-th ship, the tail as a whole (the front end of the head after) connected to the j-th queue ship where the ship. Obviously warships queue by one or more warships are in the same column composed. The combined results of the instruction execution queue will increase. However, the wily Reinhardt had made a strategic initiative. In the fighting, he can listen to instructions by the Yang Weili fleet mobilize vast intelligence network at any time. While Yang Weili issued instructions to mobilize the fleet, Reinhardt warships in order to keep abreast of current distribution Yang Weili, will issue some query commands: C ij. The directive mean, ask a computer, Yang Weili No. i and j-th battleship battleship currently in the same column, if in the same column, then arranged between them how many warships. As a veteran senior programmer, you are asked to write a program analysis Yang Weili instructions, and answer inquiries Reinhardt. The final battle has begun, the Galaxy's history has turned a page ......

Input

A first line of input integer T (1 <= T <= 500,000), T represents a total of instructions. T has the following lines, each row having one instruction. There are two instruction formats: 1.M ij: i and j are two integers (1 <= i, j <= 30000), according to the instruction number represents warships. The instruction is to mobilize the fleet command Reinhardt eavesdropped Yang Weili release, and to ensure the i-th and j-th battleship battleship not in the same column. 2.C ij: i and j are two integers (1 <= i, j <= 30000), according to the instruction number represents warships. This instruction is Reinhardt challenge issued instructions.

Output

Your program should turn on each instruction input for analysis and treatment: If the fleet is mobilized Yang Weili instructions issued, it means that the fleet arrangement has changed, your program noticed this, but do not print any information; if Reinhardt is a challenge issued instructions, your program should output one line only contains an integer representing the number of ships on the same column, the arrangement between the i-th and j-th battleship battleship. If the i-th and j-th ship ships currently not on the same column, -1 is output.

Sample Input

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

Sample Output

-1 1

answer:

I'm excited to read this topic. . .

With the right to check and set a good question(I wish every year so the water can NOI)

The so-called weighted disjoint-set is the weighted value of disjoint-set, we need to handle it and then slide down the weights when processing and check collection.

For in this question, we found that Italy had two operations merge and query, the query is something from the same set of two elements, we found that this distance is very difficult this question of a point, that we got the following operations: in a pre-merger when this distance (embodied as a pretreatment to this point from the head of the queue, so after subtraction of the last two is the distance from the head of the queue minus a ) (Do not believe your own drawing axes ).

Again, we note read.

Code:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
int fa[30001],dist[30001],sum[30001],t;//dist数组表示点到队首的距离,sum数组表示这个队中一共有多少个元素。
int find(int x)
{                                       
    if(fa[x]==x)
        return x;     
    int fx=find(fa[x]);                            
    dist[x]+=dist[fa[x]]; //累加dist                     
    return fa[x]=fx;
}//路径压缩的并查集查询操作
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=30000;i++)
    {                           
        fa[i]=i;
        sum[i]=1;
    }//初始化
    for(int i=1;i<=t;i++)
    {
        char ch;
        int x,y;
        cin>>ch>>x>>y;
        int fx=find(x);                                  
        int fy=find(y);                             
        if(ch=='M')
        {
            dist[fx]+=sum[fy];       
            fa[fx]=fy;                                    
            sum[fy]+=sum[fx];                         
            sum[fx]=0;                   
        }//每次处理
        if(ch=='C')
        {
            if(fx!=fy)
                printf("-1\n");          
            else 
                printf("%d\n",abs(dist[x]-dist[y])-1);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11318964.html