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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab自带的插值函数interp1的几种插值方法

    matlab自带的插值函数interp1的几种插值方法插值法    插值法又称“内插法”,是利用函数f(x)在某区间中已知的若干点的函数值,作出适当的特定函数,在区间的其他点上用这特定函数的值作为函数f(x)的近似值,这种方法称为插值法。如果这特定函数是多项式,就称它为插值多项式。线性插值法    线性插值法是指使用连接两个已知量的直线来确定在这两个已知量之间的一个未知量的值的方法。    假设我们已知坐标(x0,y0)与…

    2022年6月13日
    36
  • listlength函数头文件_length函数

    listlength函数头文件_length函数length函数系统默认用户浏览8778请教MySQL高手:“length()”函数与“char_length()”函数的区别是什么?高手,您好:小弟的提问如题。希望高手能够为小弟举出一个能够展示length()函数与char_length()函数区别的例子。并且配合两个函数的作用机理,为小弟做一下这两个函数区别的讲解。谢谢…高手,您好:小弟的提问如题。希望高手能够为小弟举出一个能够展示le…

    2022年6月14日
    27
  • springboot+eureka集群搭建

    springboot+eureka集群搭建eureka集群搭建此集群共有三个eureka台组成eureka集群的核心思想就是互相注册,相互守望,你中有我,我中有你(个人理解)意思就是第一台机器01注册地址defaultZone中的地址是02,03的,02的注册地址是01,03的。03的注册地址是01,02的首先新建三个项目来代表eureka的三台机器父类的pom文件大家可以一起用<?xmlversion=”1.0″encoding=”UTF-8″?><projectxmlns=”http://maven.ap

    2022年5月21日
    67
  • freemarker map 判断是否为空「建议收藏」

    freemarker map 判断是否为空「建议收藏」记录一下freemarker学习中的一些重要知识点。

    2022年6月16日
    77
  • 2017年科技界十大新闻,你都知道吗?「建议收藏」

    2017年科技界十大新闻,你都知道吗?

    2022年3月12日
    38
  • 剑指offer题目汇总_朝花夕拾题目及答案填空题

    剑指offer题目汇总_朝花夕拾题目及答案填空题目录基本都是参考别的博客和书本的代码,仅作为自己笔记用!!零、小结:一、位运算1、二进制中1的个数2、判断二进制中0的个数3.二进制高位连续0的个数二、二叉树1、二叉搜索树第k个结点2.0从上往下打印二叉树2.1二叉树打印成多行2.2按之字形顺序打印二叉树题目描述3.数据流中位数4.二叉树中和为某一值的路径5.重建二叉树6.树…

    2022年10月8日
    0

发表回复

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

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