[&& massive problem solution training && disjoint-set] Legend of the Galactic Heroes

Topic Portal

Subject 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 ......
(long title Intuit ..)


analysis:

First of all, this question tells us that there are two operations:
1 M i , j will i j 1, M_ {i, j}: the attribution belongs Fleet Fleet i j belongs
2 C i , j : i j 2, C_ {i, j}: Analyzing i, j are in the same fleet, if so, output from the two Fleet

See the two merge, query, we think of the obvious disjoint-set, so the merger, in the same query question whether the set is solved, the next step is seeking distance.

d [ i ] i We set d [i] i represents the distance of the first warships to leave
it is clear that the answer is a b s ( d [ x ] d [ y ] ) 1 abs(d[x]-d[y])-1

The question then is seeking d [ x ] d[x]

When combined, d [x] will vary with the merger, the d [x] be changed dynamically.

Thus, we think d [x] may be a change in getfa stage, i.e. distance plus distance after merging.

Well, the problem is solved


Code

#include<bits/stdc++.h>
using namespace std;
#define gc getchar()
int t;
int fa[1010100];
int d[1010100];
int num[1010100];

int getfa(int k){
    if (k==fa[k]) return k;
    int faa=getfa(fa[k]);
//    cout<<"k:"<<k<<' '<<"faa:"<<faa<<' '<<d[k]<<' '<<d[fa[k]]<<' ';
    d[k]+=d[fa[k]];
//    cout<<d[k]<<endl;
    return fa[k]=faa;
}

void zyb_is_my_granddaughter(){
    char ch;
    int x,y;
    ch=gc;
    while (ch!='M' && ch!='C') ch=gc;
    scanf("%d %d",&x,&y);
    if (ch=='M'){
	    int fax=getfa(x),fay=getfa(y);
	    fa[fax]=fay;
	    d[fax]=num[fay];
	    num[fay]+=num[fax];
//	    cout<<x<<' '<<fax<<' '<<d[fax]<<' '<<y<<' '<<fay<<' '<<num[fay]<<endl;
	}
	else 
	  if (getfa(x)!=getfa(y)) printf("-1\n");
	  else printf("%d\n",abs(d[x]-d[y])-1);
	return;
}

int main(){
	scanf("%d",&t);
	for (int i=1;i<=300000;i++) fa[i]=i,num[i]=1;
	while (t--)
	  zyb_is_my_granddaughter();
	return 0;
}

Guess you like

Origin blog.csdn.net/huang_ke_hai/article/details/94766230