• luogu P5092 [USACO2004OPEN]Cube Stacking 方块游戏


    题目描述

    约翰和贝茜在玩一个方块游戏。编号为 1…n 1ldots n 1n 的 n n n ( 1≤n≤30000 1 leq n leq 30000 1n30000 )个方块正放在地上,每个构成一个立方柱。

    游戏开始后,约翰会给贝茜发出 P P P ( 1≤P≤100000 1 leq P leq 100000 1P100000 )个指令。指令有两种:

    1. 移动(M):将包含X的立方柱移动到包含Y的立方柱上。
    2. 统计(C):统计含X的立方柱中,在X下方的方块数目。

    写个程序帮贝茜完成游戏。

    输入格式

    第1行输入 P P P ,之后 P P P 行每行输入一条指令,形式为“M X Y”或者“C X”。

    输入保证不会有将立方柱放在自己头上的指令。

    输出格式

    输出共 P P P 行,对于每个统计指令,输出其结果。

    输入输出样例

    输入 #1
    6
    M 1 6
    C 1
    M 2 4
    M 2 6
    C 3
    C 4
    输出 #1
    1
    0
    2


    元素到根的距离 记住 每次合并时才会有赋值


    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;
                }
            }
        }
    }
    刀剑映出了战士的心。而我的心,漆黑且残破
  • 相关阅读:
    larave5.6 引入自定义函数库时,报错不能重复定义
    laravel获取当前认证用户登录
    淘宝免费ip地址查询导致服务堵死的坑
    this关键字
    Jsp Spring Security 权限管理系统
    spring secrity
    spring bean何时实例化
    Spring Security3 页面 权限标签
    Spring常用注解,自动扫描装配Bean
    java继承时,实例化子类,是否会默认调用父类构造方法
  • 原文地址:https://www.cnblogs.com/OIEREDSION/p/11260182.html
Copyright © 2020-2023  润新知