题目

题目oj(洛谷)

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

给定N个方块,排成一行,将它们编号1N。再给出P个操作:

M i j表示将i所在的那一堆移到j所在那一堆的顶上。

C i表示一个询问,询问i下面有多少个方块。

•你需要写一个程序来完成这些操作。

————————————————
版权声明:本文为CSDN博主「CSYZ!!!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42434171/article/details/81711250

题解

关于这一道题目,想到使用并查集,真的是大神!!!

在这道题目中,通过阅读英文题目,发现它有并查集的影子!

在移动的时候,它是把A所在的一摞(所有的)挪动到B所在的一摞(所有的)的最上面

这就需要快速地判断哪些是在一摞上.

所以猜想使用并查集;

再想:应该把最底下的哪一个叫做根,因为比较稳定

事实上:并查集并不是仅仅用来判断是不是在一个集合里边,还可以有拓展的用途.

(比如记录某一个点到根节点的距离(在这里,我尚且把箱子的个数抽象为距离))

这个距离可以使用一次一次找爸爸的过程来进行求解, 同时还可以路径压缩

代码

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
using namespace std;
#define MAX 30009
int fa[MAX];
int dis[MAX];
int num[MAX];
int find(int x)//注意找的时候要顺便进行路径压缩,在这个过程中,dis的值也要进行更改
//由于这样,所以就不许要在单独设计一个找下面有多少个箱子的函数,直接先来一个find,直接输出dis[i]
{
if (x != fa[x])//自己不是根节点
//整个return递归采用了回溯的方法
{
int ret = find(fa[x]);
dis[x] += dis[fa[x]];
fa[x] = ret;
return ret;
}
else
{
return x;
}
}
void move(int x, int y)
{
int fx = find(x);
int fy = find(y);
fa[fx] = fy;
dis[fx] += num[fy];
num[fy] += num[fx]; } int main()
{
int T;
scanf("%d", &T);
for (int i = 1; i <= 30000; i++)
{
num[i] = 1;
fa[i] = i;
}
while (T--)
{
char buf[12];
int t1, t2;
scanf("%s", buf);
if (*buf == 'M')//移动
{
scanf("%d%d", &t1, &t2);
move(t1, t2);
}
else//查询
{
scanf("%d", &t1);
find(t1);
printf("%d\n", dis[t1]);
}
} return 0; }

Cube Stacking 来源:洛谷的更多相关文章

  1. 【洛谷P1816】忠诚——ST表做法

    看了两个小时RMQ并位运算,对二进制勉勉强强有了个初步了解,不能说精通(可能今年CSP前都做不到精通),但是记熟板子做做题还是没有问题的 以下是正式题解,相信你看过了题目,我介绍的是ST表的做法(很简 ...

  2. 洛谷P1190 接水问题

    题目名称:接水问题 题目来源 [洛谷P1190] (https://www.luogu.org/problemnew/show/P1190)​ 题目描述 学校里有一个水房,水房里一共有\(m\)个龙头 ...

  3. 加权并查集(银河英雄传说,Cube Stacking)

    洛谷P1196 银河英雄传说 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展.宇宙历七九九年,银河系的两大军事集团在 ...

  4. 洛谷1373 小a和uim之大逃离

    洛谷1373 小a和uim之大逃离 本题地址:http://www.luogu.org/problem/show?pid=1373 题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北 ...

  5. 洛谷 P1856 【Picture】

    题目描述 N(N<5000) 张矩形的海报,照片和其他同样形状的图片贴在墙上.它们的边都是垂直的或水平的.每个矩形可以部分或者全部覆盖其他矩形.所有的矩形组成的集合的轮廓称为周长.写一个程序计算 ...

  6. 洛谷P2568 GCD(线性筛法)

    题目链接:传送门 题目: 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 ...

  7. 洛谷 P1736 创意吃鱼法

    题目描述 题目链接:https://www.luogu.org/problemnew/show/P1736 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢( ...

  8. 洛谷P1387 最大正方形

    题目描述 题目链接:https://www.luogu.org/problemnew/show/P1387 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输 ...

  9. 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)

    P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...

随机推荐

  1. spring4+hibernate4 整合

    1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...

  2. Java 17 新特性:switch的模式匹配(Preview)

    还记得Java 16中的instanceof增强吗? 通过下面这个例子再回忆一下: Map<String, Object> data = new HashMap<>(); da ...

  3. Springboot目录结构分析

    1 src/main/java 存储源码 2 src/main/resource 资源文件夹    (1)src/main/resource/static 用于存放静态资源,如css.js.图片.文件 ...

  4. 吊炸天,Spring Security还有这种用法!

    在用Spring Security项目开发中,有时候需要放通某一个接口时,我们需要在配置中把接口地址配置上,这样做有时候显得麻烦,而且不够优雅.我们能不能通过一个注解的方式,在需要放通的接口上加上该注 ...

  5. java框架--快速入门

    spring快速入门    1.创建项目        1.1创建项目文件夹        1.2启动idea ->文件->打开->点击创建的项目文件夹        1.3右键创建 ...

  6. 自动化测试报告(allure/html)

    pytest有两种生成测试报告的方法(html和allure),今天就给大家一一介绍下 html 一.pytest-html基本语法 1.安装:pip install pytest-html 2.查看 ...

  7. 分布式任务调度ScheduleMaster

    1.什么是ScheduleMaster ScheduleMaster是分布式任务调度系统.简称:集中任务调度系统,最简单的理解ScheduleMaster,就是对不同的系统里面的调度任务做统一管理的框 ...

  8. 解决python 导入selenium 库后自动化运行成功但是报错问题

    本章节开始进入自动化的基础教学了,首先我们要对我们的工具有一定的熟练使用程度,做自动化常用的工具一个是搭建 RobotFramework自动化框架,另外一个便是我们最常用的python 工作原理是比较 ...

  9. Sublime text eslint windows 配置

    1. 下载安装eslint npm install -g eslint 2. 设置环境变量 C:\Users\<你的用户名>\AppData\Roaming\npm 3. sublime ...

  10. 5种在TypeScript中使用的类型保护

    摘要:在本文中,回顾了TypeScript中几个最有用的类型保护,并通过几个例子来了解它们的实际应用. 本文分享自华为云社区<如何在TypeScript中使用类型保护>,作者:Ocean2 ...