[SSL] 1225 & [Logu] P1196 Legend of the Galaxy Heroes

[SSL] 1225 & [Logu] P1196 Legend of the Galaxy Heroes

Time Limit:1000MS
Memory Limit:65536K

Description

In 5801 AD, the inhabitants of the earth moved to the second planet of Alpha Taurus, where the declaration of the founding of the Galactic Federation was issued. In the same year, it was changed to the first year of the universe and began to expand into the depths of the galaxy.
In 1979, the two major military groups in the galaxy broke out in the Pamilion star field. The Taishan Captain Group sent Cosmos Fleet Commander Reinhardt to lead more than 100,000 warships on the expedition, and Qiqiangshanhe Group named Yang Weili to organize its 30,000 warships to meet the enemy.
Yang Weili is good at arranging troops, skillfully using various tactics to win more with less, inevitably arrogant. In this decisive battle, he divided the Bamilion star field battlefield into 30000 columns, with each column numbered 1, 2, …, 30000. After that, he also numbered his warships 1, 2, …, 30000 in turn, and placed the i-th warship in the i-th column (i = 1, 2, …, 30000), forming a "snake formation with a single word". The enemy goes deep. This is the initial formation. When the invading enemy arrives, Yang Weili will issue multiple merger orders to concentrate most of the warships on a few columns and carry out intensive attacks. The merge command is M ij, which means that the entire battleship queue where the i-th battleship is located, as a whole (head in front and rear), is connected to the tail of the battleship queue where the j-th battleship is located. Obviously the battleship queue is composed of one or more battleships in the same row. The execution result of the merge instruction will increase the queue.
However, the savvy Reinhardt has already taken the initiative in strategy. During the battle, he can monitor Yang Weili's fleet movement instructions at any time through the huge intelligence network.
While Yang Weili issued instructions to mobilize the fleet, Reinhardt, in order to keep abreast of the current distribution of Yang Weili’s warships, would also issue some inquiry instructions: C ij. This command means to ask the computer whether Yang Weili’s battleship No. i and battleship No. 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 required to write programs to analyze Yang Weili’s instructions and answer Reinhardt’s queries.
The final battle has begun, and the history of the Milky Way has turned a page...

Input

The first line of the input file galaxy.in has an integer T (1<=T<=500,000), which means there are a total of T instructions.
There are T lines below, with one instruction per line. There are two formats of instructions:

  1. M ij: i and j are two integers (1<=i, j<=30000), indicating the ship number involved in the command. This order is a fleet movement order issued by Yang Weili, who was eavesdropped by Reinhardt, and to ensure that the battleship i and battleship j are not in the same row.
  2. C ij: i and j are two integers (1<=i, j<=30000), indicating the ship number involved in the command. This order is an inquiry order issued by Reinhardt.

Output

The output file is galaxy.out. Your program should analyze and process each input command in turn:
If it is a fleet movement command issued by Yang Weili, it means that the fleet arrangement has changed. Your program should pay attention to this, but do not output any information;
 if It is an inquiry command issued by Reinhardt. Your program should output a line containing only an integer, indicating the number of warships arranged between the i-th battleship and the j-th battleship in the same column. If the i-th battleship and the j-th battleship are not currently in the same row, -1 is output.

Sample Input

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

Sample Output

-1
1

Hint

WE

Ideas

This question needs to add two fields, one represents the total number of elements in the set where the element is located, represented by cnt; the other is the number of elements before this element in the set, represented by bf.
Initially,
f[i] = i;
cnt[i] = 1;
bf[i] = 0;

int find(int x)//找代表值 
{
    
    
	int t;
	if(f[x]==x) return x;
	t=find(f[x]);
	bf[x]=bf[f[x]]+bf[x];
	return f[x]=t;
}

void merge(int x,int y)//合并 
{
    
    
		x=find(x),y=find(y);
		f[x]=y;
		bf[x]+=cnt[y];
		cnt[y]+=cnt[x];
}

Code

#include<iostream>
#include<cstdio>
using namespace std;
int f[30010],cnt[30010],bf[30010];
int find(int x)//找代表值 
{
    
    
	int t;
	if(f[x]==x) return x;
	t=find(f[x]);
	bf[x]=bf[f[x]]+bf[x];
	return f[x]=t;
}
int mydabs(int x)//绝对值 
{
    
    
	if(x<0) return -x;
	return x;
}
int main()
{
    
    
	char str[10];
	int n,i,x,y;
	scanf("%d",&n);
	for(i=1;i<=30000;i++)//初始化 
		f[i]=i,cnt[i]=1,bf[i]=0;
	for(i=1;i<=n;i++)
	{
    
    
		scanf("%s%d%d",&str,&x,&y);
		if(str[0]=='M')
		{
    
    
			x=find(x),y=find(y);//合并 
			f[x]=y;
			bf[x]+=cnt[y];
			cnt[y]+=cnt[x];
		}
		else
		{
    
    
			if(find(x)!=find(y)) printf("-1\n");
			else printf("%d\n",mydabs(bf[x]-bf[y])-1);
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/113000914