luogu P5092 [USACO2004OPEN] Cube Stacking box game

Title Description

John and Bessie playing a game box. Numbered . 1. 1 ... n-\ n-ldots . 1 ... n-is NN n-( 1≤n≤30000. 1 \ n-Leq \ 30000 Leq . 1 n- . 3 0 0 0 0) n on the ground cubes, each cube constituting a column.

After the start of the game, John will Bessie issued PP P ( 1≤P≤100000 1 \ Leq P \ Leq 100000 1 P 1 0 0 0 0 0) instruction. There are two commands:

  1. Mobile (M): X containing cubic cubic moving column to the column containing the Y.
  2. Statistics (C): Column statistics Cubic containing X, the number of blocks X below.

Bessie write a program to help finish the game.

Input Format

The first input line PP P, then PP P lines each enter a command in the form of "MXY" or "CX".

Enter guaranteed not to have his head placed in the column cubic instructions.

Output Format

The output common PP P rows, for each statistics instruction, and outputs the result.

Sample input and output

Input # 1
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
Output # 1
1 
0 
2 


elements to the root of the distance from the merger remember a time when there is an assignment


code:
//
#include<bits/stdc++.h>
using namespace std;
int n,p;
#define maxnn 40100
int f[maxnn],dis[maxnn],cnt[maxnn];
int  gf(int v,int num)
{
    if(f[v]==v)
    {
        cnt[v]+=num;
        return v;
    }
    else
    {
        int fx=f[v];
        f[v]=gf(f[v],num);
        if(f[v]!=fx)
        dis[v]=dis[v]+dis[fx];
        return f[v];
    }
}
int main()
{
    char a;
    cin>>p;
    int x,y;
    for(int i=1;i<=40000;i++)
    f[i]=i,dis[i]=0,cnt[i]=1;
    
    for(int i=1;i<=p;i++)
    {
        cin>>a;
        if(a=='M')
        {
            cin>>x>>y;
            int fy=gf(y,0);
            if(gf(y,0)!=gf(x,0))
            {
            dis[gf(y,0)]=cnt[gf(x,0)];
            f[gf(y,0)]=gf(x,0);
            cnt[gf(x,0)]+=cnt[fy];
            }
        }
        else
        {
            cin>>x;
            
            {
            gf(x,0);
            cout<<abs(dis[x]-cnt[gf(x,0)])-1<<endl;
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/OIEREDSION/p/11260182.html