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


相关推荐

  • 计算机应用应届求职简历,计算机应用应届生个人简历模板

    计算机应用应届求职简历,计算机应用应届生个人简历模板计算机应用应届生个人简历模板计算机应用应届生个人简历模板一基本信息姓名:×××性别:男政治面貌:团员学历:中专毕业院校:江苏省盐城市职业技术学院专业:计算机应用技术出生日期:19xx.04.04电话:籍贯:江苏省射阳县海河镇复兴村邮箱:xxxxx工作年限:2.7工作職稱:助理领班教育经历20xx/09—20xx/07就读于江苏省盐城市职业技术学院主修课程计算机应用专业工作内容与专长1.负…

    2022年5月11日
    39
  • MODBUS协议详解

    MODBUS协议详解MODBUS 协议详解一 了解 MODBUSMODBUS 是 OSI 模型第 7 层上的应用层报文传输协议 它在连接至不同类型总线或网络的设备之间提供客户机 服务器通信 它主要用于工业自动化设备通信 MODBUS 可以在基于串行链路和以太 TCP IP 网络的 MODBUS 上可以进行通信 也就是说 可以使用串口线或者网线链接两端设备 双方约定使用 modbus 协议去通信 二 了解 MODBUS 协议前面我们说了 MODBUS 有两种实现方式 一个是串口 一个是网口 后面称呼为 TCP 那么 MODBUS 协议对应

    2025年12月10日
    4
  • GCC 命令格式

    GCC 命令格式GCC命令格式gcc[options][filenames]常用选项含义-E只做预处理-c只编译不链接,生成目标文件“.o”-S生成汇编代码-ofile把输出生成到由file指定文件名的文件中-g在输出的文件中加入支持调试的信息-v显示输出详细的命令执行过程信息GCC的主要执行步骤ELF介绍1.ELF简介注:以上内容来源于:https://www.bilibili.com/video/BV1Q5411w7z5?p=5

    2022年10月13日
    2
  • 蓝桥杯单片机必备知识—–(1)显示函数

    蓝桥杯单片机必备知识—–(1)显示函数

    2021年4月13日
    332
  • ThinkPHP函数详解:F方法

    ThinkPHP函数详解:F方法

    2021年10月9日
    35
  • java dom4j读取xml_Java DOM4J读取XML[通俗易懂]

    java dom4j读取xml_Java DOM4J读取XML[通俗易懂]DOM4J是dom4j.org出品的一个开源XML解析包。Dom4j是一个易用的、开源的库,用于XML,XPath和XSLT。它应用于Java平台,採用了Java集合框架并全然支持DOM,SAX和JAXP。Hibernate也是用它来读写配置文件的。一、DOM4j的下载dom4j-1.6.1.jar下载:http://sourceforge.net/directory/os:windows/fr…

    2022年7月26日
    5

发表回复

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

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