题目

题目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. go convert slice to struct

    Question: in golang how to convert slice to struct scene 1:use reflect convert slice to struct func ...

  2. 万字长文详解HBase读写性能优化

    一.HBase 读优化 1. HBase客户端优化 和大多数系统一样,客户端作为业务读写的入口,姿势使用不正确通常会导致本业务读延迟较高实际上存在一些使用姿势的推荐用法,这里一般需要关注四个问题: 1 ...

  3. 解构HE2E中的Kubernetes技术应用

    摘要:我们从Kubernetes技术应用的角度解构华为云DevCloud HE2E DevOps实践. 本文分享自华为云社区<解构HE2E中的Kubernetes技术应用>,作者: 敏捷小 ...

  4. GET 与 POST 其实没有什么区别

    GET 与 POST 其实没有什么区别 本文写于 2020 年 12 月 30 日 GET 与 POST 是两种 HTTP 方法,并且是最常用的两种. 今天在使用 Postman 测试 api 的时候 ...

  5. spring boot 统一接口异常返回值

    创建业务 Exception 一般在实际项目中,推荐创建自己的 Exception 类型,这样在后期会更容易处理,也比较方便统一,否则,可能每个人都抛出自己喜欢的异常类型,而造成代码混乱 Servic ...

  6. 设计并实现加法器类 Adder

    学习内容:设计并实现加法器类 Adder 代码示例: package 实验三; import java.util.Scanner; public class Adder { private int n ...

  7. 个人冲刺(三)——体温上报app(一阶段)

    任务:完成了app第二页面的页面布局 activity_second.xml <?xml version="1.0" encoding="utf-8"?& ...

  8. pandas:数据迭代、函数应用

    1.数据迭代 1.1 迭代行 (1)df.iterrows() for index, row in df[0:5].iterrows(): #需要两个变量承接数据 print(row) print(& ...

  9. [学习笔记] pd_ds黑科技

    https://www.cnblogs.com/jiqimin/p/11226809.html 丢个链接,跑路 // Author: wlzhouzhuan #pragma GCC optimize( ...

  10. DirectX11 With Windows SDK--40 抗锯齿:FXAA

    前言 在默认的情况下渲染,会看到物体的边缘会有强烈的锯齿感,究其原因在于采样不足.但是,尝试提升采样的SSAA会增大渲染的负担:而硬件MSAA与延迟渲染又不能协同工作.为此我们可以考虑使用后处理的方式 ...