题目

题目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; }

最新文章

  1. 163邮箱问题:554 DT:SPM 163 smtp5,D9GowACHO7RNWNdXmXs1Bw--.9035S2
  2. 【POJ 1182】食物链(并查集)
  3. linux 卸载软件
  4. 获取C++类成员变量的地址偏移
  5. Oracle分页查询与RowNum
  6. Kindeditor上传图片到七牛云存储插件(PHP版)
  7. spring与mybatis集成和事务控制
  8. React编写input组件传参共用onChange
  9. 如何获取Azure Storage Blob的MD5值
  10. TP传输的两种模式
  11. [Python]print vs sys.stdout.write
  12. 一致性Hash算法介绍(分布式环境算法)
  13. C++ 精英化趋势
  14. 简单实现Python调用有道API接口(最新的)
  15. nginx中的epoll模型
  16. __x__(17)0906第三天__块元素block_内联元素inline_行内块元素inline-block
  17. Git版本管理工具常用命令说明
  18. Java编程最差实践(常见编程错误典范)
  19. EZ 2018 04 06 NOIP2018 模拟赛(七)
  20. Linux wc命令详解

热门文章

  1. 面试题|Docker的优缺点
  2. vue - Vue脚手架/TodoList案例
  3. p2p-tunnel 打洞内网穿透系列(三)TCP转发访问内网web服务
  4. docker使用详解
  5. 用 Python 远程控制 Windows 服务器,太好用了!
  6. CentOS7软件环境
  7. [codeforces] 暑期训练之打卡题(二)
  8. [codeforces] 暑期训练之打卡题(一)
  9. python之三元表达式与生成式与匿名与内置函数(部分)
  10. T1创世纪(原创)