HDU 1988 Cube Stacking (数据结构-并检查集合)

HDU 1988 Cube Stacking (数据结构-并检查集合)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Cube Stacking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 18834   Accepted: 6535
Case Time Limit: 1000MS

Description

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations: 

moves and counts. 

* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y. 

* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value. 

Write a program that can verify the results of the game. 

Input

* Line 1: A single integer, P 

* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a ‘M’ for a move operation or a ‘C’ for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X. 

Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself. 

Output

Print the output from each of the count operations in the same order as the input file. 

Sample Input

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

Sample Output

1
0
2

Source



题目大意:

有N个立方体和N个格子,1~N编号,一開始i立方体在i号格子上,每一个格子刚好1个立方体。如今m组操作,M a b表示将a号立方体所在的格子的所有立方体放在b号立方体所在的格子的所有立方体上面。C x表示询问x号立方体以下的立方体的个数。

解题思路:

在并查集的基础上。仅仅须要知道x到父亲的距离以及父亲究竟的距离就知道x究竟的距离。

解题代码:

#include <iostream>
#include <cstdio>
using namespace std;

const int maxn=31000;
int father[maxn],cnt[maxn],dis[maxn];

int find(int x){
    if(father[x]!=x){
        int tmp=father[x];
        father[x]=find(father[x]);
        dis[x]+=dis[tmp];
    }
    return father[x];
}

void combine(int x,int y){
    father[x]=y;
    dis[x]+=cnt[y];
    cnt[y]+=cnt[x];
}

int main(){
    int m;
    scanf("%d",&m);
    for(int i=0;i<maxn;i++){
        father[i]=i;
        cnt[i]=1;
        dis[i]=0;
    }
    while(m-- >0){
        char ch;
        cin>>ch;
        if(ch=='M'){
            int a,b;
            scanf("%d%d",&a,&b);
            if(find(a)!=find(b)) combine(find(a),find(b));
        }else{
            int x;
            scanf("%d",&x);
            find(x);
            printf("%d\n",dis[x]);
        }
    }
    return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117330.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea2021永久激活码【中文破解版】

    (idea2021永久激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月30日
  • 408计算机考研

    408计算机考研为什么要考研?考研要先了动机,没有动机很难在枯燥且艰辛漫长的考研之路坚持到最后。有的人可能说,计算机薪资高,是钱让我充满了斗志!对此云泽只能说,真是庸俗且真实。可是你知道那掉头发的速度和你赚钱的速度是成正比的吗考研要先了动机,没有动机很难在枯燥且艰辛漫长的考研之路坚持到最后。有的人可能说,计算机薪资高,是钱让我充满了斗志!大家都知道,计算机本科就很好就业,所以我们面临的就是读研深造自己和就业的诱惑之间摇摆。我们目前主要就是数理,读研和就业的问题。读研能带给你什么东

    2022年4月30日
  • linux编辑文件命令vim怎么退出_vim退出命令

    linux编辑文件命令vim怎么退出_vim退出命令一、进入文件vim/etc/profile二、编辑文件按i进行编辑三、保存与退出1.首先按esc键返回命令编辑模式,刚才的Insert会消失2.按英文状态的:3.此时进行:q!不保存文件,强制退出vi命令:w保存文件,不退出vi命令:wq保存文件,退出vi命令4.输入以上命令按enter进行…

    2022年8月24日
  • idea的安装_theia ide

    idea的安装_theia idetheia-ide是一个能运行在浏览器里的开发环境,对常用语言都有很好的支持。非常适合在服务器上运行。本文介绍theia在docker环境下的安装和使用。首次运行基本版,包含基本的编辑功能dockerrun-it-p3000:3000-v"$(pwd):/home/project:cached"theiaide/theia:latest完整版dockerrun-it…

    2022年10月9日
  • 离散数学总复习精华版(最全 最简单易懂)已完结

    离散数学总复习精华版(最全 最简单易懂)已完结离散数学期末总复习精华版P1命题逻辑的基本概念虽然是不确定但是可以是命题就是无法判断真假优先级P2命题逻辑等值演算第一种方法:真值表求第二种用等值演算求P3命题逻辑推理理论下面给出例题后面的可以写成前提引入T12下面给出反证法附加前提证明:…………

    2022年6月18日
  • python获取当前时间的时间戳_python精确到毫秒时间戳

    python获取当前时间的时间戳_python精确到毫秒时间戳在Python中可以使用来自模块time、datetime或calendar的函数来获取当前时间戳,代码语句如【importtime;ts=time.time()print(ts)】。在Python中,有多种方法可以获得当前时间戳。如果希望在Python中获得时间戳,可以使用来自模块time、datetime或calendar的函数。使用模块time模块time是提供各种与时间相关的功能。…

    2022年10月2日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号