Cube Stacking P0J 1988(加权并查集)
Description
Write a program that can verify the results of the game.
Input
* 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
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
Sample Outpu1
0
2
题目意思:有n块方块,p次操作,两种类型,'M a b' 意思是将含有方块a的堆挪到b上:‘C a’意思是查询方块a下面有几个方块。
解题思路:这道题要使用并查集来进行计算。首先为什么会考虑使用并查集呢?因为每一次操作都是把含有a的堆挪到b上,这个时候将会把b包含到这个堆里面,重新组合成一个新的堆,这是什么?状态划分。这道题的难点主要在于虽然在了同一个集合中,但要询问这个集合内的关系,查询a下面几个方块,就可以看成其在集合中的深度。sum[n]数组来统计当前堆n中方块个数。under[n]数组来统计当前n下面的方块个数。在进行计算的时候,路径压缩和合并均更新under的值。未进行路径压缩的时候,方块n所记录的under并非final value, 仅仅只是包含了到root[n]的方块个数。 通过路径压缩的过程,不断的增加当前n的根节点(递归)的under值,求出最终的under值。进行路径合并的时候,更新sum值和under值。当一个堆放在另一个堆上时,被移动的堆的under值一定为0, 此时更新under值为另一个堆的根的sum值,即计算出了此处的部分under值。然后更新合并堆的sum值。
#include<cstdio>
#include<cstring>
#define maxs 30010
using namespace std;
int pre[maxs];
int sum[maxs];
int under[maxs];
void init()
{
int i;
for(i=; i<maxs; i++)
{
pre[i]=i;
under[i]=;
sum[i]=;
}
}
int Find(int x)
{
int t;
if(x==pre[x])
{
return x;
}
t=Find(pre[x]);
under[x]+=under[pre[x]];
pre[x]=t;
return pre[x];
}
void Union(int x,int y)
{
int a=Find(x);
int b=Find(y);
if(a==b)
{
return ;
}
pre[a]=b;
under[a]=sum[b];///将a堆放在b堆之上,更新此时a堆下面的数量
sum[b]+=sum[a];///将a堆和b堆合为一个整体,更新整体新堆的数量
} int main()
{
int n;
char q;
int a,b;
scanf("%d",&n);
getchar();
init();
while(n--)
{
scanf("%c",&q);
if(q=='M')
{
scanf("%d%d",&a,&b);
getchar();
Union(a,b);
}
else if(q=='C')
{
scanf("%d",&a);
getchar();
b=Find(a);
printf("%d\n",under[a]);
}
}
return ;
}
Cube Stacking P0J 1988(加权并查集)的更多相关文章
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- [POJ 1988] Cube Stacking (带值的并查集)
题目链接:http://poj.org/problem?id=1988 题目大意:给你N个方块,编号从1到N,有两种操作,第一种是M(x,y),意思是将x所在的堆放到y所在的堆上面. 第二种是C(x) ...
- POJ 1988 Cube Stacking(带权并查集)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 23678 Accepted: 8299 Ca ...
- POJ 1988 Cube Stacking 【带权并查集】
<题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...
- poj 1988 Cube Stacking【带权并查集】
设s[x]为x所在栈里的个数,c[x]表示x下面有几个,合并的时候直接合并s,然后路径压缩的时候更新c即可 #include<iostream> #include<cstdio> ...
- hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14
题意: 有一个运动场,运动场的坐席是环形的,有1~300共300列座位,每列按有无限个座位计算T_T. 输入: 有多组输入样例,每组样例首行包含两个正整数n, m.分别表示共有n个人,m次操作. 接下 ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- HDU 3407.Zjnu Stadium 加权并查集
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
随机推荐
- DP学习笔记
DP学习笔记 可是记下来有什么用呢?我又不会 笨蛋你以后就会了 完全背包问题 先理解初始的DP方程: void solve() { for(int i=0;i<;i++) for(int j=0 ...
- 浅淡 RxJS WebSocket
const open$ = new Subject(); const ws = webSocket({ url: 'wss://echo.websocket.org', openObserver: o ...
- Delphi无边框Form拖动
用Delphi做登陆窗口,如果使用无边框Form,想要拖动窗口,可以在某个控件的OnMouseDown事件中写下以下代码 ReleaseCapture; Perform(WM_SYSCOMMAND, ...
- SSM 框架基于ORACLE集成TKMYBATIS 和GENERATOR自动生成代码(Github源码)
基于前一个博客搭建的SSM框架 https://www.cnblogs.com/jiangyuqin/p/9870641.html 源码:https://github.com/JHeaven/ssm- ...
- go语言 os.Rename() cannot move the file to a different disk drive 怎么办
时值我小病在家休养生息,喜欢跳广场舞的外公来寻求我的帮助,他们跳广场舞是将存有歌曲的U盘插到音响上面,而音响大部分都是只能显示歌曲的索引index,不能直接显示歌曲名,所以为了方便他们会在U盘里面对歌 ...
- Hystrix使用
Hystrix是Netflix开源的一款容错系统,能帮助使用者码出具备强大的容错能力和鲁棒性的程序.如果某程序或class要使用Hystrix,只需简单继承HystrixCommand/Hystrix ...
- 20155207 2006-2007-2 《Java程序设计》第4周学习总结
20155207 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 ISP原则:一个类对另一个类的依赖应该限制在最小化的接口上. OCP原则:软件构成(类,模块 ...
- #《JAVA程序设计》 20155214 实验五 网络编程与安全
<JAVA程序设计> 20155214 实验五 网络编程与安全 实验内容 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验要求 要求一 结对实现中缀表达式转后缀 ...
- noone is not in the sudoers file ubuntu
Login as root or su to get root prompt type visudo an editor will open find a line says root ALL=( ...
- WPF 日历模板改写
原文:WPF 日历模板改写 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83346221 本 ...