AcWing238. Legend of the Galaxy Heroes

AcWing238. Legend of the Galaxy Heroes

There is an interstellar battlefield divided into N columns, each column is numbered 1, 2, ..., N.

There are N warships, which are also numbered 1, 2, ..., N sequentially, and the i-th warship is in the i-th column.

There are T instructions, and each instruction format is one of the following two:

1. M ij means to keep all the battleships in the row of the i-th battleship in the original order and connect them to the tail of the row where the j-th battleship is.

2. C ij means to ask whether the i-th battleship and the j-th battleship are currently in the same row, and if they are in the same row, how many warships are there between them.

Now you need to write a program to process a series of instructions.

Input format

The first line contains an integer T, which means there are T instructions in total.

The next T line, one instruction per line, the instruction has two forms: M ij or C ij.

Among them, M and C are capital letters to indicate the type of command, and i and j are integers, which indicate the ship numbers involved in the command.

Output format

Your program should analyze and process each input instruction in turn:

If it is in the form of M ij, it means that the fleet arrangement has changed. Your program should pay attention to this, but do not output any information;

If it is in the form of C ij, your program should output one 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 jth battleship are Currently not on the same column, -1 is output.

data range

N ≤ 30000 , T ≤ 500000 N≤30000,T≤500000 N30000,T500000

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#include<unordered_map>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
inline int gcd(int a, int b) {
    
     return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
    
     return x & -x; }


using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 30030;
int n, p[N], s[N], d[N];

int Find(int x) {
    
    
	if (p[x] != x) {
    
    
		int root = Find(p[x]);
		d[x] += d[p[x]];
		p[x] = root;
	}
	return p[x];
}

int main() {
    
    
		for (int i = 1;i < N;++i) {
    
    
			p[i] = i;
			s[i] = 1;
		}
		
		int n;scanf("%d", &n);
		while (n--) {
    
    
			char op[2];int a, b;
			scanf("%s%d%d", op, &a, &b);
			int pa = Find(a);
			int pb = Find(b);
			if (op[0] == 'M') {
    
    
				p[pa] = pb;
				d[pa] = s[pb];
				s[pb] += s[pa];
			}

			else {
    
    
				if (pa == pb)printf("%d\n", max(0, abs(d[a] - d[b]) - 1));
				else printf("-1\n");
			}
		}

	return 0;
}

Guess you like

Origin blog.csdn.net/zzq0523/article/details/113099598